zoukankan      html  css  js  c++  java
  • C#创建Windows服务

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers;
     
    namespace WindowsService1
    {
        public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
            }
     
            protected override void OnStart(string[] args)
            {
                try
                {
                    EventLog.WriteEntry("我的服务启动");
                    WriteLog("服务启动");
                    Timer t = new Timer();
                    t.Interval = 1000;
                    t.Elapsed += new ElapsedEventHandler(ChkSvr);
                    t.AutoReset = true;
                    t.Enabled = true;
     
                }
     
                catch (System.Exception ex)
                {
     
                    //错误处理  
     
                }
            }
            public void ChkSvr(object source, ElapsedEventArgs e)
            {
                try
                {
                    Timer tt = (Timer)source;
                    tt.Enabled = false;
                    SendMessahe();
                    tt.Enabled = true;
                }
                catch (Exception ex)
                {
     
                    WriteLog(ex.Message);
                }
            }
            public void SendMessahe()
            {
                try
                {
                    WriteLog("这里是要执行的任务");
                }
                catch (Exception ex)
                {
                    WriteLog(ex.Message);
                }
            }
            public void WriteLog(string read)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:/" + "ceshi.text", true);
                sw.Write("
    事件:" + read + "
    操作时间:" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "");
                sw.Close();
            }
            protected override void OnStop()
            {
                WriteLog("服务停止");
                EventLog.WriteEntry("我的服务停止");
            }
        }
    }
    在安装程序中选中【serviceProcessInstaller1】,查看其属性,将【Account】值改为【LocalSystem】。
    在安装程序中选中【serviceInstaller1】,查看其属性,将【ServiceName】值改为你想要的服务名称。 在目录【C:WindowsMicrosoft.NETFramework】中找到程序对应的.net版本 找到【InstallUtil.exe】 复制到 项目 bin/debug 下

    Install.text里面放如下代码:

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe D:C#Fanso2o_MonitoringActivityFanso2o_MonitoringActivityinDebugFanso2o_MonitoringActivity.exe
    Net Start Fanso2o_MonitoringActivity
    sc config Fanso2o_MonitoringActivity start= auto
    pause

    Uninstall.text里面放如下代码:

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u D:C#Fanso2o_MonitoringActivityFanso2o_MonitoringActivityinDebugFanso2o_MonitoringActivity.exe
    pause


    无法打开计算机“.”上的服务控制管理器。此操作可能需要其他特权。:使用管理员权限打开cmd

    再三须慎意,第一莫欺心
  • 相关阅读:
    Spring Cloud 学习推荐
    Spring-Boot-应用可视化监控
    How to Convert a Class File to a Java File?
    Nacos Cluster Building
    基于Opentracing+Jaeger全链路灰度调用链
    Opentracing + Uber Jaeger 全链路灰度调用链,Nepxion Discovery
    What happened when new an object in JVM ?
    Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials
    APK重签名总结
    C++矩阵运算库推荐
  • 原文地址:https://www.cnblogs.com/otsf/p/8582354.html
Copyright © 2011-2022 走看看