Launching Thread w/in a timer service

J

John A. Bailo

From a Windows service (NET 2.0) I want to launch serveral threads in
a for loop that invokes a method using:

new Thread(delegate()
{ myMethod(248);}).Start();


Will those threads stay active even when the service reaches it Service
Timer Stop?

Do I have to create a thread monitor to test when those threads are
finished before allowing the service to go to the next Timer_Tick?
 
J

John A. Bailo

John said:
From a Windows service (NET 2.0) I want to launch serveral threads in
a for loop that invokes a method using:

new Thread(delegate()
{ myMethod(248);}).Start();


Will those threads stay active even when the service reaches it Service
Timer Stop?

Specifically, when it reaches:

private void ServiceTimer_Tick(
object sender,
System.Timers.ElapsedEventArgs e)

{
this.timer.Stop();

[...]

}
 
M

Mr. Arnold

John A. Bailo said:
From a Windows service (NET 2.0) I want to launch serveral threads in a
for loop that invokes a method using:

new Thread(delegate()
{ myMethod(248);}).Start();


Will those threads stay active even when the service reaches it Service
Timer Stop?

Why don't you just start the thread and at myMethod, which can be for each
thread proxy/delegate, use thread.sleep() in a While True Loop?
Do I have to create a thread monitor to test when those threads are
finished before allowing the service to go to the next Timer_Tick?

You need to have a thread monitor on a timer to make sure all threads are in
a state of thread.IsAlive.
 
J

John A. Bailo

Mr. Arnold said:
Why don't you just start the thread and at myMethod, which can be for
each thread proxy/delegate, use thread.sleep() in a While True Loop?

That statement is completely unclear to me.
You need to have a thread monitor on a timer to make sure all threads
are in a state of thread.IsAlive.

So are you saying that a Timer.Stop() will cause all threads launched
within the windows service to terminate or pause?

Shouln't Timer.stop(), running on the main thread, only affect the main
thread of the application?

That is the basis of my questions.
 
J

John A. Bailo

John said:
That statement is completely unclear to me.



So are you saying that a Timer.Stop() will cause all threads launched
within the windows service to terminate or pause?

Shouln't Timer.stop(), running on the main thread, only affect the main
thread of the application?

That is the basis of my questions.

Ok. I wrote a simple test program and answered by own question.

The answer is the threads continue to run even when the timer is running
in the background.

This is super cool stuff.

That means I can pick up items in the database (tasks) that need to be
done, and have them run on threads and let my timer/main thread continue
to chug along looking for new stuff.

If I ever want to check on completion status of threads (which I don't
in this case) I could as you suggest, set up a pool and check for
completion, etc.
 
M

Mr. Arnold

class void StartAThread() //the Thread.Start() proxy/delegate
{
try
{
While True
{
Do Something();
Thread.Sleep(waittime);
}
Catch ex
{
// the catch will break out of the while on thread abort
}
}
You spawn a thread and the only way it's going yo be stopped is if you use
Thread.Stop, Thread.Pause, Thread.Sleep(0) (shut it down),
Thread.Sleep(1000) (sleep 1 second) or the thread aborts.
Ok. I wrote a simple test program and answered by own question.

The answer is the threads continue to run even when the timer is running
in the background.

This is super cool stuff.

That means I can pick up items in the database (tasks) that need to be
done, and have them run on threads and let my timer/main thread continue
to chug along looking for new stuff.

If I ever want to check on completion status of threads (which I don't in
this case) I could as you suggest, set up a pool and check for completion,
etc.

You need to abandon the Timer running in the background.

http://www.yoda.arachsys.com/csharp/threads/

I read somewhere that in using threads the While True is the method to be
used in a .NET NT Service application.
 

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