Windows Services Simple Instance

Source: Internet
Author: User

1. Timer use

    Partial classTimerservice:servicebase { PublicTimerservice () {InitializeComponent (); } Timer Timer=NewTimer (); protected Override voidOnStart (string[] args) {            //TODO: Add code here to start the service. Addlog ("* * * Service started"); Timer. Elapsed+=timer_elapsed; Timer. Interval= +; Timer.        Start (); }        intNumber =0; voidTimer_elapsed (Objectsender, Elapsedeventargs e) {            Try{ number++; Addlog ("Section"+ number +"Secondary Execution Method"); Addlog ("Current Time:"+DateTime.Now.ToLongTimeString ()); }            Catch(Exception) {}}protected Override voidOnStop () {//TODO: Add code here to perform the close operation required to stop the service. Addlog ("* * * Service terminated"); Timer.        Stop (); }         Public voidAddlog (stringstr) {            using(StreamWriter SW =NewStreamWriter (@"G:\log.data",true) ) {SW. WriteLine ("****************************************"); Sw.            WriteLine (str); }        }    }

2. Logging and database access
    Partial classmessageboxser:servicebase {Thread mainthread;  PublicMessageboxser () {InitializeComponent (); Mainthread=NewThread (NewThreadStart (ThreadFunc)); Mainthread.priority=Threadpriority.normal; }                 Public Static voidThreadFunc () {//While (true)//{            //Thread.Sleep (1000); //MessageBox.Show ("asdfasdf"); //}             while(true)            {                //MessageBox.Show ("asdfasdf");Thread.Sleep ( the); //Modify DatabaseSertableda.adddata (); //Record Log//Try//{                //using (StreamWriter SW = new StreamWriter (@ "G:\log.data", true))//    {                //SW.                WriteLine (DateTime.Now.ToLongTimeString () + "Execute loop Code"); //    }                //}                //catch (Exception ex)//{                //}            }        }        protected Override voidOnStart (string[] args) {            //TODO: Add code here to start the service. Mainthread.start (); }        protected Override voidOnStop () {//TODO: Add code here to perform the close operation required to stop the service. Mainthread.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.