zoukankan      html  css  js  c++  java
  • windows服务开发

    学然后知不足 教然后知困。

    没做过windows service开发时,感觉很难,无从下手。再网看了些例子,加上自己的理解,写下开发步骤。

    开发步骤

    1. 新建windows service

    2. 添加服务,发表后将再自己的电脑的 服务管理 查看到

    	partial class MyService : ServiceBase
    	{
    		Timer timer1;
    
    		public MyService()
    		{
    			InitializeComponent();
    		}
    
    		protected override void OnStart(string[] args)
    		{
    			// TODO: Add code here to start your service.
    
    			using (StreamWriter sw = File.AppendText(@"D:\log2.txt"))
    			{
    				sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Start.");
    			}
    
    			timer1 = new Timer();
    			timer1.Interval = 2000;
    			timer1.Elapsed += (sender, e) =>
    			{
    				using (StreamWriter sw = File.AppendText(@"D:\log2.txt"))
    				{
    					sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Countue...");
    				}
    			};
    			timer1.Start();
    		}
    
    		protected override void OnStop()
    		{
    			// TODO: Add code here to perform any tear-down necessary to stop your service.
    			using (StreamWriter sw = File.AppendText(@"D:\log2.txt"))
    			{
    				sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Stop.");
    			}
    		}
    	}
    
    1. 添加服务安装程序(这是vs给提供的方便操作),可以发表多个服务。

    右击,点击Add Installer,自动生成类ProjectInstaller。大家可以看下vs自动生成的类ProjectInstaller.Designer.cs文件

    private void InitializeComponent()
    		{
    			this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
    			this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
    			//设置服务安装信息
    			this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
    			this.serviceProcessInstaller1.Password = null;
    			this.serviceProcessInstaller1.Username = null;
    			//要安装的服务(保证与你的服务名一致,可添加多个)
    			this.serviceInstaller1.ServiceName = "MyService";
    			//服务加入
    			this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                this.serviceProcessInstaller1,
                this.serviceInstaller1});
    
    		}
    

    注意将服务安装设置为:手动安装
    4. 服务安装卸载脚本
    Install.bat 文件:

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319InstallUtil.exe "%~dp0%WindowsServiceTest.exe"
    pause
    

    Uninstall.bat文件:

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319InstallUtil.exe -u "%~dp0%WindowsServiceTest.exe"
    pause
    
  • 相关阅读:
    成功实施的APS项目故事分享---我们数据治理的心路历程
    成功实施的APS项目故事分享---如何管理与激励APS项目团队
    东莞裕同&易普优APS项目启动啦!
    包装印刷行业裕同集团&易普优APS项目顺利验收!
    易普优APS混流排序算法助力汽车整车厂的均衡生产
    动态调用WebService接口
    C#推送RocketMQ信息
    DEV带筛选行CheckBox选中问题
    验证上传文件真实格式
    PLSQL导出触发器代码
  • 原文地址:https://www.cnblogs.com/LoveTomato/p/8584026.html
Copyright © 2011-2022 走看看