Windows Service cyclic task. The restart cannot be stopped after the service is started.

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.