(+) C # Windows Services

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/knowledgesea/p/3616127.html

Introduction to Windows Services

Microsoft Windows Services can create executable applications that can run for long periods of time in their own Windows sessions. These services can be started automatically when the computer starts, can be paused and restarted, and does not display any user interface. This makes the service ideal for use on the server, or at any time, in order not to affect other users working on the same computer, which requires long-running functionality. You can also run a service in a different security context than a specific user account or a default computer account that is a logged-on user. This article describes how to use Visual C # to create a Windows service program for file monitoring step-by-step, and then describes how to install, test, and debug the Windows service program.

Create a Windows service

After you create a project--->> double-click Service1.cs---->> a design interface appears---->> right-click interface--->> pop-up dialog box to select Add Installer

After a series of actions have been completed, you can modify the Windows service Name description, startup mode, and so on.

[Runinstaller (true)] public class Installer1:System.Configuration.Install.Installer {//<summary>        The required designer variables.        </summary> private System.ComponentModel.Container components = null;        Private System.ServiceProcess.ServiceProcessInstaller Spinstaller;        Private System.ServiceProcess.ServiceInstaller Sinstaller;            Public Installer1 () {//The call is required by the designer.            InitializeComponent ();        TODO: Add any initialization after initcomponent call} #region Component Designer generated code//<summary>        The designer supports the required method-do not use the Code Editor to modify///The contents of this method. </summary> private void InitializeComponent () {components = new System.ComponentModel.            Container (); Create ServiceProcessInstaller objects and ServiceInstaller objects This.spinstaller = new System.ServiceProcess.ServiceProcessIn            Staller (); This.sinstaller = new System.ServiceProcess.            ServiceInstaller (); Set the account number, user name, and password for the ServiceProcessInstaller object this.spInstaller.Account = System.ServiceProcess.ServiceAccount.Loc            Alsystem;            This.spInstaller.Username = null;            This.spInstaller.Password = null;            Set the service name this.sInstaller.ServiceName = "Pmsdataupdateservice"; Service Description this.sInstaller.Description = "Hi Longhao!"            ";            Set the starting mode of the service this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic; This.        Installers.addrange (new system.configuration.install.installer[] {this.spinstaller, this.sinstaller}); } #endregion}

Change back and write the action you want. Service1.cs The design interface appears, double-click the design interface to enter the CS code page. You can override these methods.

  protected override void OnStart (string[] args)       {           //Service Open execute code       }       protected override void OnStop ()       {           //Service End execution Code       }       protected override void OnPause ()       {           //service suspends execution of code           base. OnPause ();       }       protected override void OnContinue ()       {           //Service Recovery Execution Code           base. OnContinue ();       }       protected override void OnShutdown ()       {           //system will close execution code           base. OnShutdown ();       }

In addition there is a Program.cs file: open to look down.

So that a Windows service program works, we need to create a program entry point for it, like creating a generic application. In the Windows service program, we also do this in the main () function. First we create an instance of the Windows service in the main () function, which should be an object of a subclass of the ServiceBase class, and then we call a run () method defined by the base class ServiceBase class. However, the run () method does not start the Windows service program, and we must complete the startup of the Windows service program by invoking a specific control function in the previously mentioned Service Control Manager, which is to wait until the OnStart () method of the object is invoked to actually start running. If you want to start multiple services at the same time in a Windows service program, you can simply define an instance object for the subclasses of multiple servicebae classes in the main () function by creating an array object of the ServiceBase class. So that each of these objects corresponds to a service that we have pre-defined.

<summary>///        The main entry point of the application.        //</summary> static void Main ()        {            servicebase[] servicestorun;            ServicesToRun = new servicebase[]             {                 new Service1 (),                new Service2 ()             };            Servicebase.run (ServicesToRun);        }

If you write the method you need in the function that you need, the point operation will not work.

Ann Loading and unloading Windows services

1, the installation needs to use, this gadget can be downloaded on the Internet.

2. Put him under the/bin/debug folder of the service program you have written.

3. Open

4. Use the command to read your service. exe folder.

5. Running Installutil.exe

6. Installation Service command: InstallUtil Yourservices.exe

7. Uninstall Service command: installutil/u Yourservices.exe

Note that: installation and uninstall need to ensure that the program is the same, no changes, to not be prompted to uninstall dirty. That is, do not modify your program in VS when the service has been installed.

Debugging Windows Services

Ensure that your service is installed successfully and in startup mode.

Point Debug--->> attach to process

Can.

Note that the following are:

Open Task Manager: Ends the process.

(+) C # Windows Services

Related Article

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.