I made this small Windows service in C #, and I believe that maybe something is wrong with my ThreadPool code, which completely disables my Windows service. If you know, a Windows service seems to work just fine when it looks at the services console, it still claims to be "starting up". When I restarted my server, the service seemed to stop again, although I set it to start automatically.
Check out my code below:
protected override void OnStart(string[] args)
{
int itemCount = itemList.Count;
this.doneEvents = new ManualResetEvent[itemCount];
for (int i = 0; i < itemCount; i++)
{
int oId = this.itemList[i];
this.doneEvents[i] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(data =>
{
while (this.activated)
{
DateTime start = DateTime.Now;
TimeSpan duration = (DateTime.Now - start);
if (duration.Milliseconds < CONST_WAITMILLISECONDS)
Thread.Sleep((CONST_WAITMILLISECONDS - duration.Milliseconds));
}
this.doneEvents[i].Set();
}, oId);
}
WaitHandle.WaitAll(doneEvents);
}
source
share