1

I have created a windows service which is currently having three timers. First timer is waking up every 15 sec, second timer is waking every min. and the third timer is waking everyday.

THe problem is these are spawning new threads every time and one time the threadpool gets used up completely.Is ther any to just spawn 3 threads and not spawn any more new threads.

My code looks something like this:

protected Onstart()
{
  var timer1  = new TImer();
  timer.Elapsed += Event1;
  timer1.interval = 60000;
  timer1.start();

  var timer2  = new TImer();
  timer2.Elapsed += Event2;
  timer2.interval = 60000;
  timer2.start();
}

private Event1(object,elapsedeventargs)
{
  var workerthread1 = **new thread**(workerthreadfunc1)
  workerthread1.start();
}

private Event2(object,elapsedeventargs)
{
  var workerthread2 = **new thread**(workerthreadfunc2)
  workerthread2.start();
}

So as you can see it is creating new threads which will use up all the threads in threadpool at some point and stops the windows service abruptly. Currently it is stopping and loogging a evet log with event ID 5000.

2
  • It may be more clear if you tell us or show us what workerthreadfuncN does. It looks like you have some work you want done every 15 seconds, every minute, and every day. As for the everyday work, this is probably better handled by some kind of task scheduler/cron job. My answer will be based on the assumption that this is your goal.
    – marr75
    Nov 18, 2009 at 16:01
  • So I have used this. ThreadPool.QueueUserWorkItem(new WaitCallback(WorkerThreadFunction1)); And I think one guy suggested that processtimerevent is already on one thread. So I have kept that in mind and will implement if this solution to use threadpool fails. –
    – alice7
    Nov 18, 2009 at 22:04

5 Answers 5

5

Your problem is not with the ThreadPool. you're creating your own threads each time one of your timers "ticks". I don't know what workerthreadfunc1 and workerthreadfunc2 are doing but if they don't return than you'll keep creating more and more threads.

If you are using System.Timers.Timer than the Elapsed event is already on a ThreadPool thread. why not perform your desired operation there?

3
  • Thnx for the advice, i hope after using threadpool the service doesn't stop again :)
    – alice7
    Nov 18, 2009 at 17:48
  • @alice: As I said, you receive the elapsed event in a thread pool's thread so you don't need to queue another work item for the pool. but I would first check if something is wrong with the thread functions. It seems like they never return.
    – Moshe Levi
    Nov 18, 2009 at 18:00
  • I am currently using try and catch in each worker thread function. But looks like it is not catching the exception at all.
    – alice7
    Nov 18, 2009 at 19:09
1

Instead of spawning new threads, use the ThreadPool.

0
1

I would change the design to not include any timers. On start, create the 3 threads, and put a loop around the work done in workerthreadfunc1 and workthreadfunc2 with a thread.sleep for the appropriate amount of time (15 seconds, 1 minute, etc...) at the beginning or end of the loop. You might want to add some check at the beginning of the loop to see if someone is trying to stop the service, or if another one has already started.

1

You could try only creating the thread if it hasn't already been created. Declare workerthread1 at the class level and do something like:

if(workerthread1 != null)
{
      workerthread1 = new thread(workerthreadfunc1);
}
2
  • That code won't work, workerthread1 hasn't been instantiated, he could make it a variable of whatever class/module he's doing this work in, however.
    – marr75
    Nov 18, 2009 at 16:09
  • Sorry. Should have read that one more time before submitting :)
    – msergeant
    Nov 18, 2009 at 16:23
1

@marr75 @zac

both of these are good advice, but why not:

int x = 0; //last exec timestamp(ts)
int s = 0; //15 s ts
int m = 0; //min ts
int d = 0; //day ts
while(check for event()){ //e.g. stop service etc.
   if((x - s)>15){
      //code for ever 15 s here
      s = currentTime();
   }
   if ((x - m)>60){
      //code for every min here
      m = currentTime();
   }
   if ((x - d)> 86400)(
      //code for every day here
      d = currentTime();
   }
   sleep(5000); // or wait() w/e sleeps the thread for 15s
   x = currentTime();
}

this however assumes that your doing a relatively light task if your doing a heavier task like a SQL query and insert or something or other just make sure the thread kills itself when done.

You could be getting possible overflow because your 15s and min worker threads take longer than 15s and a min respectively to execute, if this is true you will be adding threads faster then they terminate eventually leading to an overflow as msergeant was trying to say, only add a new thread if your old worker thread is finished.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.