一、创建Windows Service项目
1. 对服务命名(Service1——属性,设置ServiceName的值TestService)
2. 添加安装程序(服务设计界面——右击——添加安装程序)
3. 设置服务安装之后自动启动(ProjectInstaller设计界面——添加AfterInstall事件),代码如下:
public ProjectInstaller() { InitializeComponent(); //服务描述信息 this.serviceInstaller1.Description = "健康卡服务"; //服务名称和前面设置必须相同 this.serviceInstaller1.ServiceName = "TestService"; //设置服务自启动 this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; } ILog log = LogManager.GetLogger(typeof(ProjectInstaller)); /// <summary> /// 设置服务安装之后自动启动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) { try { ServiceController service = new ServiceController(this.serviceInstaller1.ServiceName); //启动服务 service.Start(); } catch (Exception ex) { log.Error("服务启动失败:" + ex); } }
二、 安装卸载服务
1. 安装服务
%SystemRoot%Microsoft.NETFrameworkv4.0.30319InstallUtil JKKService.exe pause
2. 卸载服务
net stop JKKService %SystemRoot%Microsoft.NETFrameworkv4.0.30319InstallUtil /u JKKService.exe pause
三、常用功能代码
1. 实时监控(每秒执行一次)
System.Timers.Timer timer1; public UpdatePreDischarge() { InitializeComponent(); InitService(); //记录所有未捕获的异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); } void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { try { Exception ex = e.ExceptionObject as Exception; string exmsg = "发生异常:" + ex.Message + "详细信息如下:" + Environment.NewLine + "[InnerException]" + ex.InnerException + Environment.NewLine + "[Source]" + ex.Source + Environment.NewLine + "[TargetSite]" + ex.TargetSite + Environment.NewLine + "[StackTrace]" + ex.StackTrace; SetLog(exmsg); } catch { } } /// <summary> /// 初始化服务参数 /// </summary> private void InitService() { base.AutoLog = false; base.CanShutdown = true; base.CanStop = true; base.CanPauseAndContinue = true; //服务名称,设置不一致会产生 1083 错误! base.ServiceName = "UpdatePreDischarge"; } protected override void OnStart(string[] args) { timer1 = new System.Timers.Timer(); timer1.Interval = 1000; //设置计时器事件间隔执行时间 //timer1.AutoReset = false;//设置定时器执行完毕之后,再调用timer1.Start();执行下一次。 timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); timer1.Enabled = true; } private object obj = new object(); private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { lock (obj) { //实时操作方法 TimeingExecution(); } } /// <summary> /// 实时操作方法 /// </summary> private void TimeingExecution() { } protected override void OnStop() { timer1.Enabled = false; } #region 记录日志 /// <summary> /// 记录日志 /// </summary> /// <param name="str"></param> public static void SetLog(string str) { //string log = ConfigurationManager.AppSettings["logPath"]; string log = System.Windows.Forms.Application.StartupPath + "\log.txt"; if (System.IO.File.Exists(log)) { //如果log大于100k,则删除此文件重新创建 if (new System.IO.FileInfo(log).Length > 1024 * 100) { System.IO.File.Delete(log); } } using (System.IO.StreamWriter sw = new System.IO.StreamWriter(log, true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + str); } } #endregion
版权声明:本文为博主原创文章,未经博主允许不得转载。