Recently, we need to use the Windows service to monitor the processing of some data.
Protected override void onstart (string [] ARGs)
{
While (true)
{
Try
{
Workflow. processworkflowmq ();
Thread. Sleep (150 );
}
Catch (exception E1)
{
Logger. Error (e1.message );
}
}
}
AboveCodeAfter the server is successfully installed, it cannot be started or stopped through the windows service management. For example, if you need to change the running server account, the server cannot be started.
The cause is that the onstart method cannot be executed.
The onstart method uses the multi-thread onstop method to stop the circular threads. The service can be started and stopped normally after testing.
The Code is as follows:
Protected override void onstart (string [] ARGs)
{
Logger. Error ("workflow monitoring information started! "+ Environment. newline );
Thread = new thread (New threadstart (startprocess); // enable another thread to process the business. Otherwise, the onstart method cannot be completed. The service cannot stop the startup operation.
Thread. Start ();
}
Protected void startprocess ()
{
Int I = 0;
While (true)
{
Try
{
Workflow. processworkflowmq ();
Thread. Sleep (150 );
I = 0;
}
Catch (exception E1)
{
// Logger. Error (e1.message );
// I ++;
// Thread. Sleep (500 * I );
// If (I = 200)
//{
// Thread. Abort ();
//}
}
}
}
Protected override void onstop ()
{
Thread. Abort ();
}