Start a Timer on a Thread

  • Thread starter Thread starter Harry Simpson
  • Start date Start date
H

Harry Simpson

I've been using CF for quite a while but have not goettn into the threading
portion much at all. I've found some decent articles regarding it. My boss
asked me if I could "Start a timer on a seperate thread" and I really didn't
know what to tell him - i knew we could execute a method on a thread but
figured we could also control a timer's start event too. Is there any
reason why this could not be done in CF 2?

TIA
Harry
 
Hi Harry,

If you want to access a System.Windows.Forms.Timer from a thread then you
have to use Control.Invoke(). Something like this:

static void ThreadWorker() {
this.Invoke(new EventHandler(TickerStartMethod));
}

void TimerStartMethod(object sender, EventArgs e) {
// Do stuff to your Timer...
}
 
Use the System.Threading.Timer class. That starts a thread. A Forms timer
should only be used on the primary thread.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
Harry,

just to add to what Chris said, the simple rule of thumb which is easy to
remember is; only ever use the System.Windows.Timer class within your UI
code/thread not within worker thread code and use System.Threading.Timer
otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top