Learn to write Windows Service

Source: Internet
Author: User

I remember a long time ago, a colleague had to write a scheduled task. First, when a piece of news arrives at a set timeProgramAutomatic deletion

I have no idea whether I can solve this problem.

Later, I found a method some time ago: http://www.cnblogs.com/ucetgg/archive/2009/02/11/1388481.html

This is not very good. Some senior people also replied to me and pointed out their ideas. Using Windows service, I have never written

Later I saw an articleArticle: Http://www.cnblogs.com/lovecherry/archive/2005/03/25/125527.html

I used Windows Service as a keyword to search for blogs. Many people have already done this.

I will not elaborate on the steps. Post myCodeCome out:

 

Code
Namespace Windows Service
{
Public   Partial   Class Service1: servicebase
{
Public Service1 ()
{
Initializecomponent ();
}

///   <Summary>
/// Start
///   </Summary>
///   <Param name = "ARGs"> </param>
Protected   Override   Void Onstart ( String [] ARGs)
{
// Thread t = new thread (New threadstart (run ));
// T. Start ();
Timer1.enabled =   True ;
Exectask ();

}

///   <Summary>
/// End
///   </Summary>
Protected   Override   Void Onstop ()
{
Timer1.enabled =   False ;
}

Private   Void Timersponelapsed ( Object Sender, system. Timers. elapsedeventargs E)
{
Thread t =   New Thread ( New Threadstart (exectask ));
T. Start ();
}
///   <Summary>
/// Execute task
///   </Summary>
Private   Void Exectask ()
{
String Strsql =   " Insert into website_cmscontent (title, body) values ('TT', 'xx ') " ;
Dbhelpersql. executesql (strsql );
}
}
}

 

Note:

1.Timer controls must beSystem. Timers. Timer type

2.Installutil.exe must correspond to the Framework version. For example, the installutil tool in the f ramework 2.0 program version 1.1 won't work.

3.By default, the service is not automatically started and needs to be manually started after the installation is successful.

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.