How to Start Win Long Long Service with Threading

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)
{
    // .. execute workflows ..
    eventLog.WriteEntry("Workflows executed.");

    while(alive)
    {
        Thread.Sleep(1);
        // ... check workflow states and ensure they're still executing ...
    }

    // .. halt workflow executions and perform persist operations if necessary ..
    eventLog.WriteEntry("Workflows halted.");
}

And for complete illustrative purposes, here is my implementation of OnStop ():

protected override void OnStop()
{
    alive = false;
    this.haltEvent.WaitOne(); // haltEvent is of type ManualResetEvent 
}

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-, , ?

+3
3

-, Thread ; ThreadPool. , .

-, . - , , while ( ) . , "". , WaitHandle , , .

, , ( ) , , , , , . Sleep; ?

+4

Windows, OnStart() System.Timers.Timer ElapsedEventHandler.

, , , , , . OnStop() .

, , , , . .

+2

, , , Wait/Pulse Programming . .

Wait and Pulse - Threading #

0

Source: https://habr.com/ru/post/1756759/


All Articles