Summary of creating Windows service with. NET (C # code) tojike (original)

Source: Internet
Author: User
Tags bool functions
Window| Create a summary of Windows service creation with. NET Tojike (original)

Keyword Windows service



Objective

NET provides a dedicated class library for creating Windows service, ending the embarrassment of previous Windows service development. You can even use wizard to generate a Windows service without adding a single line of code.

First, using wizard to generate the most basic framework



At this point, the system will generate a framework for you, some of the main source code is as follows:

Using System;

Using System.Collections;

Using System.ComponentModel;

Using System.Data;

Using System.Diagnostics;

Using System.ServiceProcess;

Namespace WindowsService1

{

public class Service1:System.ServiceProcess.ServiceBase

{



Private System.ComponentModel.Container components = null;



Public Service1 ()

{

InitializeComponent ();

}

static void Main ()

{

System.serviceprocess.servicebase[] ServicesToRun;

ServicesToRun = new system.serviceprocess.servicebase[] {new Service1 ()};

System.ServiceProcess.ServiceBase.Run (ServicesToRun);

}

private void InitializeComponent ()

{

components = new System.ComponentModel.Container ();

This. ServiceName = "Service1";

}

protected override void Dispose (bool disposing)

{

if (disposing)

{

if (Components!= null)

{

Components. Dispose ();

}

}

Base. Dispose (disposing);

}

protected override void OnStart (string[) args)

{



}

protected override void OnStop ()

{



}

}

}



It is necessary to explain its structure. Among them, System.ServiceProcess is the key, is the introduction of Windows service place. The OnStart (), OnStop () Two functions can be invoked by Windows Service Manager or MMC to start and stop the service.

Second, building a service

The framework provides an empty service and nothing can be done. So we're going to add code to it. For example, I want to

A service that scans the database requires a second interval after each scan, and then continues the scan.

According to the above requirements, the preliminary idea needs a timer class, check the namespace, found there are two different timer classes, they are:

1, System.Windows.Forms.Timer

2, System.Timers.Timer

And a system.threading, with a sleep method.

Which one should we use?

Check MSDN, will find only 2 fit, for 1, Timer control time is not accurate, for thread said, implementation is more difficult.

Third, the planning process

Based on the requirements of the service, the process of determining the service is as follows:



To do this, we define two functions: _scan (bool _judge), _do_something ()

The System.Timers namespace is then introduced and a _timer object is defined, as follows:

1, using System.Timers; Introduction of System.Timers

2, public System.Timers.Timer _timer; Define Object _timer

3, public bool _scan (bool _judge)

              {                                                                                                                                                                     

Todo

              }                                                                                                                                                                    

4. public void _do_something ()

              {                                                                                                                                                                   

Todo

}



Then add the _timer elapsed event in InitializeComponent () with the following code:

This._timer. Elapsed + = new System.Timers.ElapsedEventHandler (this._timer_elapsed);

Define the _timer_elapsed event with the following code:

private void _timer_elapsed (object sender, System.Timers.ElapsedEventArgs e)

{

_timer. interval=1000;

_timer. Enabled=false;

if (_scan () ==true)

{

_do_something ();

}

_timer. Enabled=true;

}

Finally, let's not forget to add the Windows Service's installer, also a wizard, and basically don't need to add a line of code. Then compile and build an executable file. Note: Because this is not an ordinary executable file, it cannot be run by double-clicking, only through InstallUtil yourservicename, net START yourservicename, net STOP yourservicename, installutil/u yourservicename to install, start, stop, suspend (optional), uninstall the service. It is best to do batch processing files, once and for all. ^_^



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.