473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Timer doesn't work in seperate thread

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
4 3342
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mr. B | last post by:
My current app has a timer that I kick ON in my Form1_Load as follows: ' Set Up the Timer Function Dim t As New System.Timers.Timer(12000) ' 1000 = 1 Second t.Enabled = True ' False to Turn OFF...
3
by: Peter Johnsson | last post by:
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you assign a new value to the timer objects...
8
by: Daniel P. | last post by:
I'm trying to set a timer that gets called every 3 seconds so I can update a field in the UI with the time elapsed since the process started. What am I doing wrong that timerDF_Tick does not get...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
3
by: aprivate | last post by:
Hi I am having some problem with a form timer. I added a progress bar to increment its value and the timer works when I use form1.showdialog() However when I use form1.show, form1.refresh()...
4
by: Lemune | last post by:
Hello everyone. I'm using vb 2005. I'm creating program that run as service on windows. And in my program I need to use timer, so I'm using timer object from component. I try my source code on...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
4
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
4
by: =?Utf-8?B?Y2FzaGRlc2ttYWM=?= | last post by:
Hi, I have an application which does some file manipulation. While the file manipulation takes place, the timer in the statusbar freezes. How can I tell the timer to run on a different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.