By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Problems with timer-object

P: n/a
Hi everybody

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

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
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Nijazi,

There are for timers (three real ones)
threading.timers.timer
system.timers.timer
windowsforms.timer

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

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor

"Nijazi Halimaji" <sp***@gmx.net> schreef in bericht
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi everybody

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

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

Nov 21 '05 #2

P: n/a
"Nijazi Halimaji" <sp***@gmx.net> schrieb:
I have created a new timer object
WithEvents tmr_check As New System.Timers.Timer

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."


'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 'SynchronizingObject' 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/>

Nov 21 '05 #3

P: n/a
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 SynchronizingObject. Thanks.
--
Dennis in Houston
"Herfried K. Wagner [MVP]" wrote:
"Nijazi Halimaji" <sp***@gmx.net> schrieb:
I have created a new timer object
WithEvents tmr_check As New System.Timers.Timer

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."


'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 'SynchronizingObject' 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/>

Nov 21 '05 #4

P: n/a
"Dennis" <De****@discussions.microsoft.com> schrieb:
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?


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/>

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.