G
Guest
I have a Windows Service with a Timer object with an interval of 500 ms, in
each event the windows service starts a new thread, up to a limited number of
threads, and does some work.
It works fine as to the users requirements, but sometimes freezes. The
Service is running, its process is visible on Task Manager as usually, no
events on the Event Log are related to the service, but the work is not done.
After some tests I foun out that the the code associated with the OnTimer
Event stops being executed with no apparent reason.
I'm clueless....
Some code:
public class QueueHandler : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.Container components = null;
private System.Timers.Timer timer = new System.Timers.Timer();
private Thread[] serviceThreads;
protected override void OnStart(string[] args)
{
serviceThreads = new Thread[25];
int interval = 500;
string autoThreads = "no";
timer.Elapsed += new ElapsedEventHandler(OnTimer);
timer.Interval = interval;
timer.Enabled = true;
}
protected void OnTimer(Object Source,ElapsedEventArgs e)
{
int x = GetNextFreeThread();
if (x >= 0)
serviceThreads[x].Start();
}
private int GetNextFreeThread()
{
int threadID = -1;
MessageProcessor mProcessor = new MessageProcessor();
ThreadStart tStart = new ThreadStart(mProcessor.ProcessMessage);
for (int y = 0;y<serviceThreads.Length;y++)
{
//if thread slot is free instaciate new thread
if(serviceThreads[y] == null || serviceThreads[y].ThreadState ==
System.Threading.ThreadState.Stopped)
{
threadID = y;
serviceThreads[threadID] = new Thread(tStart);
serviceThreads[threadID].Name = "QueueThread_" + y.ToString();
serviceThreads[threadID].IsBackground = true;
serviceThreads[threadID].Priority = ThreadPriority.Lowest;
break;
}
}
return threadID;
}
each event the windows service starts a new thread, up to a limited number of
threads, and does some work.
It works fine as to the users requirements, but sometimes freezes. The
Service is running, its process is visible on Task Manager as usually, no
events on the Event Log are related to the service, but the work is not done.
After some tests I foun out that the the code associated with the OnTimer
Event stops being executed with no apparent reason.
I'm clueless....
Some code:
public class QueueHandler : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.Container components = null;
private System.Timers.Timer timer = new System.Timers.Timer();
private Thread[] serviceThreads;
protected override void OnStart(string[] args)
{
serviceThreads = new Thread[25];
int interval = 500;
string autoThreads = "no";
timer.Elapsed += new ElapsedEventHandler(OnTimer);
timer.Interval = interval;
timer.Enabled = true;
}
protected void OnTimer(Object Source,ElapsedEventArgs e)
{
int x = GetNextFreeThread();
if (x >= 0)
serviceThreads[x].Start();
}
private int GetNextFreeThread()
{
int threadID = -1;
MessageProcessor mProcessor = new MessageProcessor();
ThreadStart tStart = new ThreadStart(mProcessor.ProcessMessage);
for (int y = 0;y<serviceThreads.Length;y++)
{
//if thread slot is free instaciate new thread
if(serviceThreads[y] == null || serviceThreads[y].ThreadState ==
System.Threading.ThreadState.Stopped)
{
threadID = y;
serviceThreads[threadID] = new Thread(tStart);
serviceThreads[threadID].Name = "QueueThread_" + y.ToString();
serviceThreads[threadID].IsBackground = true;
serviceThreads[threadID].Priority = ThreadPriority.Lowest;
break;
}
}
return threadID;
}