可视化Windows服务定时任务
思路:1、新建一个《Windows服务项目》,在里面建服务文件(可以多个),用来做要执行的任务。
2、新建一个《Windows窗体应用程序》,在这个程序里管理服务。如:安装、卸载、启动、停止。
示例(定时写日志):
1、新建解决方案,如图:
2、LogService里新建服务文件(可以建多个,一个服务文件就是一个服务):
3、打开服务文件,右键:
4、设置属性:
5、在服务文件(WriteLog)里写要执行的任务:
using System; using System.ServiceProcess; using System.Timers; namespace LogService { partial class WriteLog : ServiceBase { private Timer timer = new Timer(); public WriteLog() { InitializeComponent(); } protected override void OnStart(string[] args) { //服务开启执行代码 timer.Enabled = true; timer.Interval = 1000; timer.Elapsed += timer_Elapsed; } protected void timer_Elapsed(object sender, ElapsedEventArgs e) { if (DateTime.Now.Second == 1) { LogHelper.ErrorLog("这里是要做的事", "D:\\", "log.txt"); } } protected override void OnStop() { //服务结束执行代码 timer.Enabled = false; } protected override void OnPause() { //服务暂停执行代码 } protected override void OnContinue() { //服务恢复执行代码 } protected override void OnShutdown() { //系统即将关闭执行代码 } } }
6、服务的Program.cs里这样配置(可以配置多个服务文件):
using System; using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace LogService { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WriteLog()//这里可以配置多个服务文件 }; ServiceBase.Run(ServicesToRun); } } }
7、服务制作完成,在应用程序里新建一窗体,用于对服务进行控制:
8、窗体源码:
using System; using System.ServiceProcess; using System.Windows.Forms; namespace LogServiceSetup { public partial class 定时任务 : Form { private string serviceExe = "LogService.exe"; private string serviceName = "WriteLog"; public 定时任务() { InitializeComponent(); if (ServiceAPI.ExistService(serviceName)) { 安装.Enabled = false; ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Stopped || service.Status == ServiceControllerStatus.Paused) { label1.Text = "服务未启动"; 启动.Enabled = true; 卸载.Enabled = true; 停止.Enabled = false; } if (service.Status == ServiceControllerStatus.Running) { label1.Text = "服务正在运行"; 启动.Enabled = false; 停止.Enabled = true; 卸载.Enabled = false; } } else { label1.Text = "服务未安装"; 安装.Enabled = true; 卸载.Enabled = false; 启动.Enabled = false; 停止.Enabled = false; } } private void 安装_Click(object sender, EventArgs e) { try { ServiceAPI.Installter(serviceExe); label1.Text = "服务已安装"; 安装.Enabled = false; 卸载.Enabled = true; 启动.Enabled = true; 停止.Enabled = false; } catch (Exception ex) { label1.Text = ex.Message; } } private void 卸载_Click(object sender, EventArgs e) { try { ServiceAPI.Uninstall(serviceExe); label1.Text = "服务已卸载"; 安装.Enabled = true; 卸载.Enabled = false; 启动.Enabled = false; 停止.Enabled = false; } catch (Exception ex) { label1.Text = ex.Message; } } private void 启动_Click(object sender, EventArgs e) { try { ServiceAPI.Start(serviceName); label1.Text = "服务已启动"; 安装.Enabled = false; 卸载.Enabled = false; 启动.Enabled = false; 停止.Enabled = true; } catch (Exception ex) { label1.Text = ex.Message; } } private void 停止_Click(object sender, EventArgs e) { try { ServiceAPI.Stop(serviceName); label1.Text = "服务已停止"; 安装.Enabled = false; 卸载.Enabled = true; 启动.Enabled = true; 停止.Enabled = false; } catch (Exception ex) { label1.Text = ex.Message; } } } }
9、ServiceAPI源码:
using System; using System.Configuration.Install; using System.Reflection; using System.ServiceProcess; namespace LogServiceSetup { public class ServiceAPI { //服务是否存在 public static bool ExistService(string serviceName) { bool exist = false; ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { if (service.ServiceName == serviceName) { exist = true; break; } } return exist; } // 安装服务 public static void Installter(string serviceExe) { try { string serviceFileName = Assembly.GetExecutingAssembly().Location; string[] cmdline = { }; TransactedInstaller transactedInstaller = new TransactedInstaller(); AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceExe, cmdline); transactedInstaller.Installers.Add(assemblyInstaller); transactedInstaller.Install(new System.Collections.Hashtable()); } catch (Exception ex) { throw ex; } } // 卸载服务 public static void Uninstall(string serviceExe) { try { string[] cmdline = { }; TransactedInstaller transactedInstaller = new TransactedInstaller(); AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceExe, cmdline); transactedInstaller.Installers.Add(assemblyInstaller); transactedInstaller.Uninstall(null); } catch (Exception ex) { throw ex; } } //启动服务 public static void Start(string serviceName) { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Stopped || service.Status == ServiceControllerStatus.Paused) { service.Start(); try { service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 20)); } catch (Exception ex) { throw ex; } } } //暂停服务 public static void Pause(string serviceName) { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Running) { service.Pause(); try { service.WaitForStatus(ServiceControllerStatus.Paused, new TimeSpan(0, 0, 20)); } catch (Exception ex) { throw ex; } service.Close(); } } //恢复服务 public static void Continue(string serviceName) { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Paused) { service.Continue(); try { service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 20)); } catch (Exception ex) { throw ex; } } } //停止服务 public static void Stop(string serviceName) { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Running) { service.Stop(); try { service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 20)); } catch (Exception ex) { throw ex; } service.Close(); } } } }
7、把服务生成的文件(\bin\debug\)copy到应用程序中(\bin\debug\)。
8、运行应用程序: