By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,926 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,926 IT Pros & Developers. It's quick & easy.

Timer doesn't work in seperate thread

P: n/a
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

Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"redneon" <re*****@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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

Nov 16 '05 #2

P: n/a
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!");
}

"redneon" <re*****@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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

Nov 16 '05 #3

P: n/a
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.

--
Roman S. Golubin
ICQ UIN: 63253392

PS: Sorry for my bad english
Nov 16 '05 #4

P: n/a
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

"Abubakar" wrote:
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
"redneon" <re*****@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.