Using InvokeRequired with a Timer control and a second thread
-
Hello, I am a bit stuck with my timer control. My timer control works in the main thread. Now i have other threads running doing some stuff. This second thread needs to enable the timer. Well the timer.Enabled is indeed true, but i don't get events, it doesn't tick. I read along the web to use the InvokeRequired. Well, i can't figure out how to use it. Can someone please put me on the right track. Thanks in advance.
-
Hello, I am a bit stuck with my timer control. My timer control works in the main thread. Now i have other threads running doing some stuff. This second thread needs to enable the timer. Well the timer.Enabled is indeed true, but i don't get events, it doesn't tick. I read along the web to use the InvokeRequired. Well, i can't figure out how to use it. Can someone please put me on the right track. Thanks in advance.
You need Control.InvokeRequired and Control.Invoke when you want to access a Control from within a thread that did not create the Control (normally the main or "GUI" thread). Most timers run their event in some thread, and hence need Invoke to touch Controls; by design Forms.Timer does not. If you want to know more about it, read up on it in MSDN, some good articles on CP, or Google. There is a simple example (C#) burried inside my Sokoban article. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
You need Control.InvokeRequired and Control.Invoke when you want to access a Control from within a thread that did not create the Control (normally the main or "GUI" thread). Most timers run their event in some thread, and hence need Invoke to touch Controls; by design Forms.Timer does not. If you want to know more about it, read up on it in MSDN, some good articles on CP, or Google. There is a simple example (C#) burried inside my Sokoban article. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Hello, Thanks for your answer. It is indeed quiet simple. I read the following article on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconServerBasedTimers.asp And I replaced my Windows Timer to a Server Timer, problem solved. Thank you. ESTANNY