Home All Groups Group Topic Archive Search About

New Problem with timers

Author
28 Mar 2005 9:17 PM
George Thompson
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

Author
29 Mar 2005 11:52 AM
Nick Hall
Show quote Hide quote
"George Thompson" <GeorgeThomp***@discussions.microsoft.com> wrote in
message news:6A748F44-CE42-4113-AFB2-8381F393D6D7@microsoft.com...
> 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
Author
30 Mar 2005 6:11 PM
George Thompson
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


Show quoteHide quote
"Nick Hall" wrote:

>
> "George Thompson" <GeorgeThomp***@discussions.microsoft.com> wrote in
> message news:6A748F44-CE42-4113-AFB2-8381F393D6D7@microsoft.com...
> > 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
>
>
>