zoukankan      html  css  js  c++  java
  • .Net 开发Windows Service

    1、首先创建一个Windows Service 

    2、创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释:

    using System;

    using System.IO;

    usingSystem.ServiceProcess;

    using System.Text;

    usingSystem.Timers;

    namespaceTestService

    {

       public partial class Service1 : ServiceBase

       {

           public Service1()

           {

               InitializeComponent();

           }

           protected override voidOnStart(string[] args)

           {

               //服务开启执行代码

               StartDoSomething();

           }

           protected override void OnStop()

           {

               //服务结束执行代码

           }

           protected override void OnPause()

           {

               //服务暂停执行代码

               base.OnPause();

           }

           protected override void OnContinue()

           {

               //服务恢复执行代码

               base.OnContinue();

           }

           protected override void OnShutdown()

           {

               //系统即将关闭执行代码

               base.OnShutdown();

           }

           private void StartDoSomething()

           {

               System.Timers.Timer timer = newSystem.Timers.Timer(10000); //间隔10秒

               timer.AutoReset = true;

               timer.Enabled = false;  //执行一次

               timer.Elapsed += newElapsedEventHandler(WriteSomething);

               timer.Start();

           }

           private void WriteSomething(objectsource, System.Timers.ElapsedEventArgs e)

           {

               FileStream fs = null;

               try

               {

                   fs = newFileStream("d:/1.txt", FileMode.OpenOrCreate);

                   string strText = @"//实例化一个文件流--->与写入文件相关联

                   FileStream fs = newFileStream(sf.FileName, FileMode.Create);

                   //实例化一个StreamWriter-->与fs相关联

                   StreamWriter sw = newStreamWriter(fs);

                   //开始写入

                   sw.Write(this.textBox1.Text);

                   //清空缓冲区

                   sw.Flush();

                   //关闭流

                   sw.Close();

                   fs.Close();";

                   //获得字节数组

                   byte[] data = newUTF8Encoding().GetBytes(strText);

                   //开始写入

                   fs.Write(data, 0, data.Length);

                   //清空缓冲区、关闭流

                   fs.Flush();

                   fs.Close();

                   fs.Dispose();

               }

               catch

               {

               }

               finally

               {

                   if (fs != null)

                   {

                       fs.Close();

                       fs.Dispose();

                   }

               }

           }

       }

    }

    3、然后切换到设计视图,右键点击下图中圈选的“添加安装程序”

    4、选中下图第一个控件,点击F4,右边切换到属性视图;更改属性视图中的Account属性为LocalService(本地服务)

    5、

    选中上面第二个控件,点击F4,右边切换到属性视图。更改ServiceName为你自己喜欢的服务名称,记住不要和系统的冲突了哦~,亲!StartType默认为手动,你可以更改为自动

    (Automatic)或禁用(Disabled)

    6、编译项目,然后win+R输入cmd进入命令窗口。去对应.net版本下的目录中找到InstallUtil.exe,我项目采用的是 .net 2.0,故路径为C:WINDOWSMicrosoft.NETFrameworkv2.0.50727

    Cmd命令
    installutil      WindowsService_test.exe  (安装Windows服务)
    installutil /u   WindowsService_test.exe  (卸载Windows服务)

  • 相关阅读:
    思念
    空白
    curl json string with variable All In One
    virtual scroll list All In One
    corejs & RegExp error All In One
    socket.io All In One
    vue camelCase vs PascalCase vs kebabcase All In One
    element ui 表单校验,非必填字段校验 All In One
    github 定时任务 UTC 时间不准确 bug All In One
    input range & color picker All In One
  • 原文地址:https://www.cnblogs.com/mumue/p/4923362.html
Copyright © 2011-2022 走看看