zoukankan      html  css  js  c++  java
  • 使用.net 创建windows service

    最近公司项目需要,写了个windows 服务,windows 服务的内容可以在VS 中新建一个“windows服务项目”,

     (1)服务中的主要代码:

        public partial class Service1 : ServiceBase     {        

          public Service1()      

         {          

           InitializeComponent();            

           TaskToRun();                  

         }

          protected override void OnStart(string[] args)      {    

             }

       

         protected override void OnStop()         {

            }

           private void TaskToRun() {

                TimerCallback tcb = new TimerCallback(writeToLocal);           

               AutoResetEvent autoEvent = new AutoResetEvent(false);            

             // 每隔5分钟执行一次            

           System.Threading.Timer timer = new Timer(tcb, autoEvent, 10000, 60000 * 5);        

            }

            private void writeToLocal(object objectstate)        

            {            

                string path = ConfigurationManager.AppSettings["savePath"].ToString();

                using (StreamWriter writer = File.AppendText(path))         

               {  writer.WriteLine("程序执行时间:" + DateTime.Now.ToString());            

                }         

              }    

          }

    (2)在service1 中右击点击“添加安装程序”:

      

    (3)在ServiceIntall1 中编辑服务名称,并在ServiceProcessIntall1 中将“Account”属性修改为“LocalSystem”

    (4)编译生成项目文件并将bin 文件部署到服务器上。

    (5) 在要部署的服务器上安装installUtil.exe:

    以我的机器(安装的是.net framework 4.0)为例,打开命令行,运行如下命令来安装服务:

    cd c:windowsMicrosfot.NetFrameworkV4.0.30319

       intallutil.exe D:WinserviceTest1WinserviceTest1.exe

      

       (6) 这样WinserviceTest1 服务安装完成后,就可以在windows“服务”列表中找到该服务并启动:

     (7)最后可以查看到该服务的运行结果:

      

    (8)参考资料:

    1)C#创建、安装一个Windows服务:

    http://blog.csdn.net/yysyangyangyangshan/article/details/10515035

    2) [转]C#创建Windows服务与安装

    http://www.cnblogs.com/pingfanren/archive/2015/11/22/4986053.html

  • 相关阅读:
    Google 面试准备清单
    Two sorted array. Find kth smallest element, 要求O(logK)
    MVC(demo)
    UE4学习心得:Scene Component蓝图的一个简单应用
    UE4中如何使物体始终朝向摄像头?
    响应式Web设计
    Nodejs的express使用教程
    安装express遇到的问题
    致自己
    上传文件的方法
  • 原文地址:https://www.cnblogs.com/Fluent-1202/p/6226701.html
Copyright © 2011-2022 走看看