首先
一、创建Windows服务
修改项目名称
重命名Service名称
右键在空白区域点击 添加安装程序
此时软件会生成两个组件,分别为“serviceInstaller1”及“serviceProcessInstaller1”,如下图所示:
首先设置serviceProcessInstaller1 User改为LocalSystem
点击“servicestaller1”,在“属性”窗体将ServiceName改为MyService,Description改为我的服务,StartType保持为Manual,如下图所示:
F7进入代码编辑页面 Service页面 编辑好生成一下
code
public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
}
/// <summary>
/// 开始服务
/// </summary>
string filePath = @"E:\MyServiceLog.txt";
protected override void OnStart(string[] args)
{
using (FileStream stream = new FileStream(filePath, FileMode.Append))
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine($"{DateTime.Now},服务启动!");
}
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(TimedEvent);//每隔自定的间隔时间就会触发一次事件,若不暂停将无限循环触发,相当于一个计时器。
timer.Interval = 1000 * 5; //每5秒执行一次
timer.Enabled = true; //是否启用
}
private void TimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
Task.Run(() =>
{
//封装好的方法
GetAllOrder getAllOrder = new GetAllOrder();
getAllOrder.GetAll();
});
}
/// <summary>
/// 停止服务
/// </summary>
protected override void OnStop()
{
using (FileStream stream = new FileStream(filePath, FileMode.Append))
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine($"{DateTime.Now},服务停止!");
}
}
}
在bin根目录文件夹下会出现 MyWindowsService.exe文件
在MyWindowsService的App.config里设置数据库链接
二、创建安装、启动、停止、卸载服务的Windows窗体
在WinForm应用程序添加新建项 添加应用程序清单文件app.manifest
设置app.manifest 把level="asInvoker"设置成level="requireAdministrator"
设置控件
F7进入代码编辑页面
code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string serviceFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\MyWindowsService.exe";
string serviceName = "MyService";
//事件:安装服务
private void Install_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
this.InstallService(serviceFilePath);
}
//事件:启动服务
private void StartUp_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
}
//事件:停止服务
private void StopOff_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
}
//事件:卸载服务
private void UnInstall_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
}
//判断服务是否存在
private bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
}
//安装服务
private void InstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
}
}
//卸载服务
private void UninstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
}
}
//启动服务
private void ServiceStart(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
}
}
}
//停止服务
private void ServiceStop(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
}
}