C # Write Windows services and install and start

Source: Internet
Author: User
A program that Windows services automatically opens at system startup is ideal for background handlers.
1), create Windows Service project
In the new project, select the Windows service.
Implement the OnStart and OnStop inside, and add the corresponding transaction operation flow. If there are other requirements, you can set the properties, such as pausing, and then overload the corresponding interface.
OnStart can not do too time-consuming operations (if the 30s cannot be completed, Windows will assume that startup failed), so if there are complex operations, you need to complete through the thread, and as soon as possible from the onstart back.
OnStart can receive a string array parameter, at startup, you can pass the Servicecontroller.start (...). Incoming
2), add installation program
Right-click on the design interface of the service and choose ' Add Installer ' from the pop-up menu to add installation-related features. In the design interface of the installation, Set up ServiceInstaller-related properties (primarily the service name ServiceName and startup type StartType) and ServiceProcessInstaller related properties (mainly the installation account accounts).
Setup account if set to user, you need to set the login name ( the format for the./+ AccountWith the login password, otherwise the installation will eject the Password Input Verification dialog box. If you have other types of installation accounts, you need to be aware of permissions issues (which may cause client access to be denied).
3), installation and writing
The easiest way to do this is through the InstallUtil command, which can be used directly by launching the SDK command interface, but not for automatic installation implementations.
Here is a description of the coding implementation. Implemented through reflection, so the implemented services must add the installer through the second version above. When installing and uninstalling, you need to first determine if the service is installed. _strlogsrvname is the service name.
private bool Issrvexisted (string strsrvname_)
{
Try
{
ServiceController Srvctrl = new ServiceController (strsrvname_);
ServiceControllerStatus Eustatu = srvctrl.status;
return true;
}
catch (SystemException) {}

return false;

servicecontroller[] Srvctrls = servicecontroller.getservices ();
foreach (ServiceController srv in srvctrls)
//      {
if (SRV. ServiceName = = strsrvname_)
return true;
//      }
//
return false;
}

public static void Install (String strsrvfile_)
{
if (issrvexisted (_strlogsrvname))
{
Return
}

System.Collections.Hashtable hashstate = new System.Collections.Hashtable ();
Assemblyinstaller asinst = new Assemblyinstaller ();
Asinst.usenewcontext = true;
Asinst.path = Strsrvfile_;
Asinst.install (hashstate);
Asinst.commit (hashstate);
}

public static void Uninstall (String strsrvfile_)
{
if (issrvexisted (_strlogsrvname))
{
Assemblyinstaller asinst = new Assemblyinstaller ();
Asinst.usenewcontext = true;
Asinst.path = Strsrvfile_;
Asinst.uninstall (NULL);
}
}
4), service start and stop
Look through Windows Service Manager for easy startup (if you need parameters at startup, this way is not possible) and stop
public static void Start (int nosid_, string strdbconfigfile_)
{
ServiceController Srvctrl = new ServiceController (_strlogsrvname);
if ((Srvctrl.status = = servicecontrollerstatus.stopped)
|| (Srvctrl.status = = servicecontrollerstatus.stoppending))
{
Srvctrl.start (new string[] {nosid_. ToString (), strdbconfigfile_});
Wait for seconds
Srvctrl.waitforstatus (servicecontrollerstatus.running, New TimeSpan (0, 0, 30));
}
}

public static void Stop ()
{
ServiceController Srvctrl = new ServiceController (_strlogsrvname);
if ((Srvctrl.status!= servicecontrollerstatus.stopped)
&& (Srvctrl.status!= servicecontrollerstatus.stoppending))
{
Srvctrl.stop ();
}
}
5), service debugging
The above completes a basic service to write, but the service can not interact, how to debug it.
In order to facilitate debugging, you need to Add a Sleep 20-second (sleeping) statement to the OnStart
The program compiles well, no problem; Install breakpoints in the service program (to be behind sleep)
Start the service. Select our service process in IDE ' debug '-' Attach to process ' (we need to be sure to do it in 20 seconds).     After the load is complete, wait (wait for the sleep to complete), the program jumps to the breakpoint, and then you can debug it. Of course, in addition to the above methods, you can also write logs, write files to debug.

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.