在做一些計劃任務時候難免用到Windows Service服務程序,而這個是沒有操作界面的,每次啟動、重啟、關閉都需要服務界面找到服務進行操作,對普通的人來說是非常麻煩的,所以有時候就需要通過應用程序來控制Windows 服務,這裡把之前寫到的一個服務控制類貼出來。
C# Windows 服務控制類代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Threading;
namespace Tools.App
{
public class ServiceHelper
{
///
/// Restart windows service
///
///the windows service display name
/// If the restart successfully return true else return false
public static bool RestartWindowsService(string serviceName)
{
bool bResult = false;
try
{
try
{
StopWindowsService(serviceName);
Thread.Sleep(1000);
}
catch (Exception ex)
{
StartWindowsService(serviceName);
Thread.Sleep(1000);
StopWindowsService(serviceName);
Thread.Sleep(1000);
Console.WriteLine(ex.Message);
}
try
{
StartWindowsService(serviceName);
Thread.Sleep(1000);
}
catch (Exception ex)
{
StopWindowsService(serviceName);
Thread.Sleep(1000);
StartWindowsService(serviceName);
Thread.Sleep(1000);
Console.WriteLine(ex.Message);
}
bResult = true;
}
catch (Exception ex)
{
bResult = false;
throw ex;
}
return bResult;
}
///
/// Start windows service
///
///the windows service display name
/// If the start successfully return true else return false
public static bool StopWindowsService(string serviceName)
{
ServiceController[] scs = ServiceController.GetServices();
bool bResult = false;
foreach (ServiceController sc in scs)
{
if (sc.ServiceName == serviceName)
{
try
{
sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(1000 * 30));
sc.Stop();
bResult = true;
}
catch (Exception ex)
{
bResult = false;
throw ex;
}
}
}
return bResult;
}
///
/// Stop windows service
///
///the windows service display name
/// If the stop successfully return true else return false
public static bool StartWindowsService(string serviceName)
{
ServiceController[] scs = ServiceController.GetServices();
bool bResult = false;
foreach (ServiceController sc in scs)
{
if (sc.ServiceName == serviceName)
{
try
{
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(1000 * 30));
sc.Start();
bResult = true;
}
catch (Exception ex)
{
bResult = false;
throw ex;
}
}
}
return bResult;
}
public static bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}
}
}
窗體按鈕事件調用代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
namespace Tools.App
{
public partial class ServiceForm : Form
{
public ServiceForm()
{
InitializeComponent();
}
///
/// 加載判斷服務是否存在
///
///
///
private void ServiceForm_Load(object sender, EventArgs e)
{
if (ServiceHelper.ServiceIsExisted(TaskTimer))
{
ServiceController service = new ServiceController(TaskTimer);
if (service.Status != ServiceControllerStatus.Stopped && service.Status != ServiceControllerStatus.StopPending)
{
this.btnStart.Enabled = false;
this.button2.Enabled = true;
}
else
{
this.btnStart.Enabled = true;
this.button2.Enabled = false;
}
}
}
///
/// 啟動
///
///
///
private void btnStart_Click(object sender, EventArgs e)
{
ServiceHelper.StartWindowsService(TaskTimer);
this.btnStart.Enabled = false;
this.button2.Enabled = true;
MessageBox.Show(啟動成功, 提示);
}
///
/// 關閉
///
///
///
private void btnClose_Click(object sender, EventArgs e)
{
ServiceHelper.StopWindowsService(TaskTimer);
this.btnStart.Enabled = true;
this.button2.Enabled = false;
MessageBox.Show(關閉成功, 提示);
}
private void btnIsExisted_Click(object sender, EventArgs e)
{
bool bl = ServiceHelper.ServiceIsExisted(TaskTimer);
if (bl)
{
MessageBox.Show(TaskTimer 存在!, 提示);
}
else
{
MessageBox.Show(TaskTimer 不存在!, 提示);
}
}
}
}