Problems with timer-object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nijazi Halimaji

    Problems with timer-object

    Hi everybody

    I have created a new timer object
    WithEvents tmr_check As New System.Timers.T imer

    On my form_Load-Event I activated the timer...

    Then I made a label on my form and putted on the Elapsed-Event of my timer
    object following code to change the Label.Text
    Label1.Text = "Ola"

    When this event is executed, following error occurs:
    "Cross-thread operation not valid: Control 'Label1' accessed from a thread
    other than the thread it was created on."

    I know that I have to set the label.text on the right thread, but how to do?
    Never did this before!

    Thanks for every help

    Nijazi Halimaji


  • Cor Ligthert [MVP]

    #2
    Re: Problems with timer-object

    Nijazi,

    There are for timers (three real ones)
    threading.timer s.timer
    system.timers.t imer
    windowsforms.ti mer

    The last one are too in the toolbox, in my opinion it is better to use with
    windowforms applications the window.forms.ti mer.



    I hope this helps,

    Cor

    "Nijazi Halimaji" <spami@gmx.ne t> schreef in bericht
    news:%23yseFd0w FHA.1252@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Hi everybody
    >
    > I have created a new timer object
    > WithEvents tmr_check As New System.Timers.T imer
    >
    > On my form_Load-Event I activated the timer...
    >
    > Then I made a label on my form and putted on the Elapsed-Event of my timer
    > object following code to change the Label.Text
    > Label1.Text = "Ola"
    >
    > When this event is executed, following error occurs:
    > "Cross-thread operation not valid: Control 'Label1' accessed from a thread
    > other than the thread it was created on."
    >
    > I know that I have to set the label.text on the right thread, but how to
    > do? Never did this before!
    >
    > Thanks for every help
    >
    > Nijazi Halimaji
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Problems with timer-object

      "Nijazi Halimaji" <spami@gmx.ne t> schrieb:[color=blue]
      > I have created a new timer object
      > WithEvents tmr_check As New System.Timers.T imer
      >
      > On my form_Load-Event I activated the timer...
      >
      > Then I made a label on my form and putted on the Elapsed-Event of my timer
      > object following code to change the Label.Text
      > Label1.Text = "Ola"
      >
      > When this event is executed, following error occurs:
      > "Cross-thread operation not valid: Control 'Label1' accessed from a thread
      > other than the thread it was created on."[/color]

      'System.Timers. Timer' runs on another thread than the applications UI does.
      Instance members of Windows Forms controls are not safe for multithreading,
      and thus you cannot access them directly from another thread.
      'System.Timers. Timer' has a 'SynchronizingO bject' property. If you set this
      property to your form, the problem should be solved. Note that
      'System.Windows .Forms.Timer' has been designed especially for use in Windows
      Forms applications. Maybe this is the better choice in this situation.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • Dennis

        #4
        Re: Problems with timer-object

        I have read that using the Windows.Timer doesn't fire exactly at the specific
        timer intervals you set because of Windows being busy processing something
        else when the timer times out for the event. However, the System.Timer runs
        in a separate thread and fires the Elasped event in a more predicatble
        manner...is this true?

        I tried to convert to Systems Timer but had problems with threads getting
        mixed up so glad to hear about the SynchronizingOb ject. Thanks.
        --
        Dennis in Houston


        "Herfried K. Wagner [MVP]" wrote:
        [color=blue]
        > "Nijazi Halimaji" <spami@gmx.ne t> schrieb:[color=green]
        > > I have created a new timer object
        > > WithEvents tmr_check As New System.Timers.T imer
        > >
        > > On my form_Load-Event I activated the timer...
        > >
        > > Then I made a label on my form and putted on the Elapsed-Event of my timer
        > > object following code to change the Label.Text
        > > Label1.Text = "Ola"
        > >
        > > When this event is executed, following error occurs:
        > > "Cross-thread operation not valid: Control 'Label1' accessed from a thread
        > > other than the thread it was created on."[/color]
        >
        > 'System.Timers. Timer' runs on another thread than the applications UI does.
        > Instance members of Windows Forms controls are not safe for multithreading,
        > and thus you cannot access them directly from another thread.
        > 'System.Timers. Timer' has a 'SynchronizingO bject' property. If you set this
        > property to your form, the problem should be solved. Note that
        > 'System.Windows .Forms.Timer' has been designed especially for use in Windows
        > Forms applications. Maybe this is the better choice in this situation.
        >
        > --
        > M S Herfried K. Wagner
        > M V P <URL:http://dotnet.mvps.org/>
        > V B <URL:http://classicvb.org/petition/>
        >
        >[/color]

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Problems with timer-object

          "Dennis" <Dennis@discuss ions.microsoft. com> schrieb:[color=blue]
          >I have read that using the Windows.Timer doesn't fire exactly at the
          >specific
          > timer intervals you set because of Windows being busy processing something
          > else when the timer times out for the event. However, the System.Timer
          > runs
          > in a separate thread and fires the Elasped event in a more predicatble
          > manner...is this true?[/color]

          Yes, that's true. The "Remarks" section of the documentation for
          'System.Timers. Timer' says:

          | Server timers can move among threads to handle the
          | raised 'Elapsed' event, resulting in more accuracy than
          | Windows timers in raising the event on time.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          Working...