473,621 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 21 '05 #1
4 3066
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.

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

I hope this helps,

Cor

"Nijazi Halimaji" <sp***@gmx.ne t> schreef in bericht
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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

Nov 21 '05 #2
"Nijazi Halimaji" <sp***@gmx.ne t> schrieb:
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."


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

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 SynchronizingOb ject. Thanks.
--
Dennis in Houston
"Herfried K. Wagner [MVP]" wrote:
"Nijazi Halimaji" <sp***@gmx.ne t> schrieb:
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."


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

Nov 21 '05 #4
"Dennis" <De****@discuss ions.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
2679
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 left. When i get it to display a complete countdown (days hours mins seconds left) it is one hour short when counting dates that fall in the following time brackets: 2004,2,29 -> 2004,9,31 2005,2,28 -> 2005,9,30 2006,2,27 -> 2006,9,29 etc..
5
1699
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 released as needed, runs fine for some time, then stops - Visual Studio detects no exceptions, and I have placed break points on all the handling code to catch them. No errors / failures are logged, but child threads have all exited, and I am left...
6
2898
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. When I make calls to SetWindowLong and SetLayeredWindowAttributes using the window handle for a poup form in Access, and call from within a timer event handler, it works most of the time, but sometimes will stop the timer from firing until I click...
4
4020
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. When timers fire, main form is somewhat blocking until timers finish their job, (socket operations). (Imagine to move the form by it's caption bar and it somewhat freeze when timers fire...)
12
7517
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 public sub I'm doing: Public Sub ThisIsMySub(TheseAreMyArguments) Me.Show Me.WindowState = FormWindowState.Normal Me.TopMost = True
2
4396
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 seems a little silly. Anyone know how to circumvent this? Right now I am having the function that is bound to the double click event undo the action the single click event performs. This is annoying and it flashes the single click event for a...
4
4990
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 creates a DLL. I am able to build the project without any probs in the IDE, however - when I build the project from the command line (using the same options shown in the 'Command line' node in the 'Project Settings' dialog box), I get the following...
0
890
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 the periodicity for refreshing the details/progress of the Windows service. This all works perfectly in my test environment running 1.1. The production environment runs off the 2.0 framework. The Windows service (which uses no timers) runs fine,...
3
1383
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. The code within the Return_Answer function called another function called Timer which is in dll_1) dll2 (Main dll function is called Calculate_Best_Result. The code
4
2446
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 the keyboard port. The problem comes from the use of the default MessageBox whenever I need to display information to the operator: DialogResult dr =
0
8213
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8597
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5554
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4065
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1460
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.