C # create a Windows service series 3-create a windows service with a controllable Interface)

Source: Internet
Author: User

In the previous two articles, we can automatically start windows and regularly execute windows service

Series 1: Create a Windows service that is installable, can be started, can be stopped, and can be uninstalled (downmoon original)

Series 2: demonstrate a regularly executed windows service and debug (windows service) (downmoon)

Series 3: windows service series 3-create a windows service with a controllable Interface

This article will focus on how to control windows Services that are invisible to the naked eye through the interface. In fact, the main classes used are:

ServiceController. You can refer to the official instructions. If you are eager to see the results, please read them directly.

Preparation: assume that we have prepared a wondows service and deployed it. The service name is pingServiceDemo,
  
Next we will create a controllable interface to operate this windows service.

Start:

Step 1: Create a winform project named CtrlPingService and add a reference to System. ServiceProcess. dll.

  

Step 2: define variables and add button events. The main code is as follows:

Private System. Windows. Forms. Button btnStart;
Private System. Windows. Forms. Button btnStop;
Private System. Windows. Forms. Button btnExit;
Private System. ServiceProcess. ServiceController sController;
Private Label lbInfo;
Private Button ftnPause;
Private Button btnResume;
Private Label lbServiceName;
Private TextBox txtServerName;
Private Button btnServiceState;
Private string serverName;

# Region Ecents

Private void frmMain_Load (object sender, EventArgs e)
{
If (serverName = null) {serverName = txtServerName. Text. Trim ();}
LbInfo. Text = "the current service status is:" + getStateForService (serverName );
}
Private void btnServiceState_Click (object sender, EventArgs e)
{
LbInfo. Text = "the current service status is:" + getStateForService (serverName );
}
Private void btnStart_Click (object sender, System. EventArgs e)
{
RunService (serverName );
}
Private void btnStop_Click (object sender, System. EventArgs e)
{
KillService (serverName );
}
Private void ftnPause_Click (object sender, EventArgs e)
{
PauseService (serverName );
}
Private void btnResume_Click (object sender, EventArgs e)
{
ResumeService (serverName );
}
Private void btnExit_Click (object sender, System. EventArgs e)
{
This. Close ();
Application. Exit ();
}
# Endregion

Step 3: add the method used. The Code is as follows:

# Region Methods

Private void runService (string servername)
{
ServiceController SC = new ServiceController (servername );
ServiceControllerStatus st = SC. Status;
Switch (st)
{
Case ServiceControllerStatus. StopPending:
Case ServiceControllerStatus. Stopped:
SC. Start ();
Break;
Default: break;
}
SC. WaitForStatus (ServiceControllerStatus. Running );
St = SC. Status; // obtain the service Status again
If (st = ServiceControllerStatus. Running)
{
LbInfo. Text = "service" + SC. ServiceName + "started! ";
}
}
Private void killService (string servername)
{
ServiceController SC = new ServiceController (servername );
ServiceControllerStatus st = SC. Status;
Switch (st)
{
Case ServiceControllerStatus. Running:
Case ServiceControllerStatus. StartPending:
Case ServiceControllerStatus. Paused:
Case ServiceControllerStatus. PausePending:
Case ServiceControllerStatus. ContinuePending:
SC. Stop ();
SC. WaitForStatus (ServiceControllerStatus. Stopped );
Break;
}
St = SC. Status; // obtain the service Status again
If (st = ServiceControllerStatus. Stopped)
{
LbInfo. Text = "service" + SC. ServiceName + "stopped! ";
}
}
Private void pauseService (string servername)
{
ServiceController SC = new ServiceController (servername );
ServiceControllerStatus st = SC. Status;
Switch (st)
{
Case ServiceControllerStatus. Running:
Case ServiceControllerStatus. StartPending:
SC. Pause ();
SC. WaitForStatus (ServiceControllerStatus. Paused );
Break;
}
St = SC. Status; // obtain the service Status again
If (st = ServiceControllerStatus. Paused)
{
LbInfo. Text = "service" + SC. ServiceName + "is suspended! ";
}
}
Private void resumeService (string servername)
{
ServiceController SC = new ServiceController (servername );
ServiceControllerStatus st = SC. Status;
Switch (st)
{
Case ServiceControllerStatus. Paused:
Case ServiceControllerStatus. PausePending:
SC. Continue ();
SC. WaitForStatus (ServiceControllerStatus. Running );
Break;
}
St = SC. Status; // obtain the service Status again
If (st = ServiceControllerStatus. Running)
{
LbInfo. Text = "service" + SC. ServiceName + "has been continued! ";
}
}
Private string getStateForService (string servername)
{
ServiceController SC = new ServiceController (servername );
ServiceControllerStatus st = SC. Status;
Return SC. Status. ToString ();
}
# Endregion

OK! Finally, let's take a look at our achievements:

  

  

  
Invitation month Summary: in fact, WCF can do this completely. Here is just a simple demonstration. You may be able to write about the WCF series later. Haha

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.