Timer doesn't work in seperate thread

G

Guest

I've set up a seperate thread and put a timer in it but for some reason it's
tick event is never fired. This is what I have...

....
Thread timerThread = new Thread(new ThreadStart(startTimer));
timerThread.Start();
....

private void startTimer()
{
System.Windows.Forms.Timer idleTimer = new System.Windows.Forms.Timer();
idleTimer.Interval = 1000;
idleTimer.Tick += new System.EventHandler(idleTimer_Tick);
idleTimer.Start();
}

private void idleTimer_Tick(object sender, System.EventArgs e)
{
System.Console.WriteLine("Time Reached!");
}

If I put a breakpoint on the WriteLine statement it never gets run and I
can't figure out why.

Any ideas?

Darrell
 
A

Abubakar

I havnt ran the code but it looks like the thread is aborted before your
timer can raise its tick event. After "idleTimer.Start();" you see that the
function exits which destroys the thread. You need to hold the thread and
keep it running. One way is not use a timer at all. The thread can do it
itself. Inside your startTimer() function do the following:

while (true ) // you can handle a loop terminating condition here as u like
{
Thread.Sleep( 1000 );
idleTimer_Tick(null, null);

}
this way the tick function will get called like a timer event after every
1000 mili secs.

Ab.
hope that helps.
http://joehacker.blogspot.com
 
D

Dan Bass

try declaring the timer in the class, not just within the scope of the
"startTimer()" method. Like this:
------

...
Thread timerThread = new Thread(new ThreadStart(startTimer));
timerThread.Start();
...

System.Windows.Forms.Timer idleTimer;
private void startTimer()
{
idleTimer = new System.Windows.Forms.Timer();
idleTimer.Interval = 1000;
idleTimer.Tick += new System.EventHandler(idleTimer_Tick);
idleTimer.Start();
}

private void idleTimer_Tick(object sender, System.EventArgs e)
{
System.Console.WriteLine("Time Reached!");
}
 
R

Roman S. Golubin

Hi, Darrell!

[skip]

System.Windows.Forms.Timer idleTimer;
private void startTimer()
{
idleTimer = new System.Windows.Forms.Timer();
idleTimer.Interval = 1000;
idleTimer.Tick += new System.EventHandler(idleTimer_Tick);
idleTimer.Start();
}

private void idleTimer_Tick(object sender, System.EventArgs e)
{
System.Console.WriteLine("Time Reached!");
}

If I put a breakpoint on the WriteLine statement it never gets run and I
can't figure out why.

Any ideas?

The idleTimer variable exists only while startTimer procedure runs...
At the moment either startTimer procedure is over then all local variables
(idleTimer) is out of the thread scope... all resourses ( and idleTimer
variable ) were distroyed and event nothing raised.
take out the idleTimer varibale from the procedure and be happy.
 
G

Guest

Thanks for the reply but I've sorted it. I didn't even need to use a seperate
thread. I just use a System.Timers.Timer instead of a
System.Windows.Forms.Timer and it works fine for some reason.

Darrell
 

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

Top