New Problem with timers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

I am following text book timer usage as defined in the on-line VB help. The
timer works partly. I'm using the following timer code segment in my
application. :

Public Sub MyTimer(ByVal sender As Object, ByVal e As ElapsedEventArgs)

lblStatText.Text = ">> Processing - " & Str(nr)
MsgBox("On timer" & Str(nr), MsgBoxStyle.OKOnly)

End Sub

The problem is this... I can't get the lblStatText.Text line to display in
my form. I think the application is hanging when it gets to this line. If I
comment the lblStatText... line out the MsgBox line works just fine.

I've rewritten the line to use a text box instead and still have the same
problem.

I've done everything with lblStatText to insure that it is spelled corrected
and works. It works fine in other subs just not in the timer handler.

Any ideas?

Thanks,
George
 
George Thompson said:
Hi All,

I am following text book timer usage as defined in the on-line VB help.
The
timer works partly. I'm using the following timer code segment in my
application. :

Public Sub MyTimer(ByVal sender As Object, ByVal e As ElapsedEventArgs)

lblStatText.Text = ">> Processing - " & Str(nr)
MsgBox("On timer" & Str(nr), MsgBoxStyle.OKOnly)

End Sub

The problem is this... I can't get the lblStatText.Text line to display in
my form. I think the application is hanging when it gets to this line. If
I
comment the lblStatText... line out the MsgBox line works just fine.

I've rewritten the line to use a text box instead and still have the same
problem.

I've done everything with lblStatText to insure that it is spelled
corrected
and works. It works fine in other subs just not in the timer handler.

Any ideas?

Thanks,
George
Hi George,

According to the documentation, the timer that I think you are using
(System.Timers.Timer) is intended for SERVER applications. The reason your
code is failing is because the event is raised on a thread other than the
main GUI thread. All windows controls must be accessed from the GUI thread
only. If this rule is broken, things may not work or runtime exceptions may
be thrown.

There is a similar component which is intended for Windows Forms
applications which provides similar functionality. Have a look at
System.Windows.Forms.Timer.

Hope this helps,

Nick Hall
 
Thanks Nick,

I investige your idea.

I have been working with the following code sample that I got from somewhere
on the internet. This sample is basically what I am trying to duplicate in my
application. This
sample runs fine.

Public Class Form1
Inherits System.Windows.Forms.Form

Private t As New System.Timers.Timer

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

AddHandler t.Elapsed, AddressOf TimerFired
End Sub

Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
t.Interval = 2000
t.Enabled = True
End Sub

Public Sub TimerFired(ByVal sender As Object, _
ByVal e As System.Timers.ElapsedEventArgs)

Label2.Text = "Signal Time = " & Str(1)
End Sub


Thanks again.

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top