• WINDOWS服务安装(C#)


    1、新建服务项目

    比如

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Threading;
    using System.Windows.Forms;
    namespace CatchArticle
    {
     public class CatchArticle : System.ServiceProcess.ServiceBase
     {
      /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      private System.ComponentModel.Container components = null;
      private Thread MainThread;
      public CatchArticle()
      {
       // 该调用是 Windows.Forms 组件设计器所必需的。
       InitializeComponent();

       MainThread=new Thread(new ThreadStart(ThreadFunc));
        MainThread.Priority=ThreadPriority.Lowest;
      }

      // 进程的主入口点
      static void Main()
      {
       System.ServiceProcess.ServiceBase[] ServicesToRun;
       ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CatchArticle() };
       System.ServiceProcess.ServiceBase.Run(ServicesToRun);
      }

      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器
      /// 修改此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {
       components = new System.ComponentModel.Container();
       this.ServiceName = "CatchArticle";
      }

      /// <summary>
      /// 清理所有正在使用的资源。
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }

      /// <summary>
      /// 设置具体的操作,以便服务可以执行它的工作。
      /// </summary>
      protected override void OnStart(string[] args)
      {
       MainThread.Start();
      }
     
      /// <summary>
      /// 停止此服务。
      /// </summary>
      protected override void OnStop()
      {
       MainThread.Abort();
      }

      public static void ThreadFunc()
       {
         int LastHour=DateTime.Now.Hour;
         while (true)
         {
          System.Threading.Thread.Sleep(1000);
          if (DateTime.Now.Hour-1==LastHour)
          {
            MessageBox.Show("为了爱护您的眼睛,请您暂时休息5分钟并向远处眺望!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
         LastHour=DateTime.Now.Hour;
          }
         }
       }

     }
    }

    ========================================

    2、然后增加一个ProjectInstall.cs 继承自Installer ,里面的一些参数可以自己定义

    using System.Collections;
    using System.Configuration.Install;
    using System.ServiceProcess;
    using System.ComponentModel;

    namespace CatchArticle
    {
     /// <summary>
     /// myInstall 的摘要说明。
     /// </summary>
     ///
     [RunInstaller(true)]
     public class myInstall : Installer
     {

      private ServiceInstaller serviceInstaller;
      private ServiceProcessInstaller processInstaller;
      public myInstall()
      {
       processInstaller = new ServiceProcessInstaller();
       serviceInstaller = new ServiceInstaller();

       processInstaller.Account = ServiceAccount.LocalSystem;
       serviceInstaller.StartType = ServiceStartMode.Automatic;
       serviceInstaller.ServiceName = "CatchArticle";

       Installers.Add(serviceInstaller);
       Installers.Add(processInstaller);
      }
     }
    }

    3、然后便以Release

    4、使用C:\......\Framework\v1.1..\installUtil.exe C:\项目路径\bin\release\TestService.exe

    注意:如果不增加ProjectInstall.cs,则会报如下错误

    正在安装程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
    受影响的参数是:
       assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
       logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
    没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
    正在提交程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
    受影响的参数是:
       assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
       logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
    没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
    没有安装程序,因此移除 InstallState 文件。
    正在安装程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
    受影响的参数是:
       assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
       logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
    没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
    正在提交程序集“f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe”。
    受影响的参数是:
       assemblypath = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe
       logfile = f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.InstallLog
    没有 RunInstallerAttribute 的公共安装程序。在 f:\夜隼网站目录\edzh.com\windowsservice\bin\debug\windowsservice.exe 程序集中可能可以找到“Yes”属性。
    没有安装程序,因此移除 InstallState 文件。

  • 相关阅读:
    记录ubuntu安装nvidia-drivers简单的做法
    Win 与 Ubuntu 双系统时间同步问题解决
    记录安装mmdetection
    记录ubuntu 16.04安装matlab 2016b的过程
    vmware ubuntu 16.04 扩容 根目录
    记录一下使用qt designer和pyqt5做界面的过程
    提升模型性能的几个方法以及训练集测试集验证集的划分与作用
    Markdown语法 (转载)
    CatBoost: gradients boosting with categorical features support
    Gradient Centralization: A New Optimization Technique for Deep Neural Networks
  • 原文地址:https://www.cnblogs.com/yesun/p/431242.html
Copyright © 2020-2023  润新知