I have a payoff service in which there are several workflows (WorkflowApplication and WorkflowServiceHost) that I need to continue working. Since OnStart () requires it to exit and return to the OS, I have a main method that runs in another thread in threadpool. My Onstart () basically looks like this
protected override void OnStart(string[] args)
{
eventLog.WriteEntry("Service starting...");
ThreadPool.QueueUserWorkItem(new WaitCallback(ServiceMainThread));
Thread.Sleep(100);
eventLogCms.WriteEntry("Service Started.");
}
ServiceMainThread () is a method in which my workflows and basic functionality are executed. When I start the service on my machine with Windows 7, it starts and then dies after about 8 minutes. In Win Server 2008, a thread NEVER executes.
So, I think that I implemented threads incorrectly and that ServiceMainThreadshakes a little. I am open to suggestions about what can be improved or some direction, as I am new to streaming in .Net. The base thread code in ServiceMainThread is encoded as such:
private void ServiceMainThread(object state)
{
eventLog.WriteEntry("Workflows executed.");
while(alive)
{
Thread.Sleep(1);
}
eventLog.WriteEntry("Workflows halted.");
}
And for complete illustrative purposes, here is my implementation of OnStop ():
protected override void OnStop()
{
alive = false;
this.haltEvent.WaitOne();
}
Is there something obvious that I could change so that my workflows remain in the long run? The while loop seems a bit too hacky (not to mention the fact that I don't like to hold the thread for a while, as it is), and I'm sure there might be a better solution.
, , 2 , OnStop(). , while ServiceMainThread() , , .
Update:
.Net :
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
at Ptm.ServiceMainThread()
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
2008 - 64-, , ?