473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problems with timer-object

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
4 3052
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bonnett | last post by:
I have been creating a generic countdown timer (source code below), counting the seconds, minutes, hours and days till an event, but I have having trouble with it finding out how many hours are...
5
by: Rob Durant | last post by:
Hi, I have a multi-threaded application (have also tried as service - same behaviour) that runs fine on XP, but not on 2003. Symptoms are: Threads are started normally, locks acquired and...
6
by: Steve Jorgensen | last post by:
I know quite well that this question falls into the category of "why does Access misbehave when I do unexpected things to its objects?", but I thought I'd ask anyway, and see if anyone knows. ...
4
by: Anthony Boudouvas | last post by:
Hi to all, i have a form with 2 System.Windows.Forms.Timer objects. One fire every 5 seconds and the other every 10 seconds, the both take actions in two hashtables declared in same form. ...
12
by: E Goforth | last post by:
Hello, I have a VB6 app that calls a VB.NET app via named pipes and a third party component. Inside the VB.NET app I'm trying to force a form to the top, at least temporarily. Inside a form's...
2
by: scott_gui | last post by:
I am creating a Windows application: The mouse event <Double-Button-1> has a conflict when the <Button-1> event also has a binding. Double clicks will first perform the single click action. This...
4
by: Bit byte | last post by:
I have a project that I normally build (without problems) from the DevStudio IDE. However, I have embarked on automating all my builds (this test project being one of several). The project...
0
by: John Mason | last post by:
Hello, I have a small client app using a timer control that communicates with a Windows service using .NET remoting. Both apps were authored against the 1.1.4322 framework. The timer controls...
3
by: stenasc | last post by:
Hi, I have created an C based application in linux that uses a number of dlls. Within the dlls themselves, the code calls sub functions e.g dll_1 (Main dll function is called Return_Answer....
4
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got an application that takes input from a handheld scanner: Once the incoming text meets the RegEx format and is the correct length, the command is accepted. The scanner that plugs in on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.