English 中文(简体)
Threadpool is getting used by windows service problem
原标题:

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.

最佳回答

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?

问题回答

Instead of spawning new threads, use the ThreadPool.

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.

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);
}

@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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签