Windows服务

参考地址:https://www.cnblogs.com/2828sea/p/13445738.html

1. 新建服务

 2. 在 service 下 添加安装程序

会自动添加

 修改这两个文件属性:

serviceInstaller1:

  • DelayedAutoStart:是否自动启动
  • Descrition:介绍服务(自定义)
  • DisplayName:标识服务器友好名称(自定义)== ServerName
  • StartType:Automatic(自动启动)重点

serviceProcessInstaller1:

  • Account:设置为LocalSystem(运行此服务的账户类型)

 

在 server1设计界面拉一个 系统日志事件

此系统日志可以在此查看 

 Service1代码,

 public Service1()
        {
            InitializeComponent();
            //系统日志
            eventLog1 = new System.Diagnostics.EventLog();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MySource", "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";

            Init();
        }

        private void Init()
        {
            try
            {
                _IntervalTime = Convert.ToInt32(GetConfig.GetConfigByFileName("WindowsService", "IntervalTime"));
            }
            catch(Exception ex)
            {
                LogControl.Error(ex.Message);
            }
        }

        //计时器
        System.Threading.Timer _Timer;

        //委托方法
        delegate void StartTime(object obj);

        int _IntervalTime = 1;

        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart.");
            LogControl.Debug("Come in OnStart");
            _Timer = new System.Threading.Timer(new TimerCallback(Start), this, 1, _IntervalTime * 60 *1000);
        }

        private void Start(object obj)
        {
            eventLog1.WriteEntry("run timer");
            LogControl.Debug("execute time:" + DateTime.Now.ToString());
        }


        protected override void OnStop()
        {
            eventLog1.WriteEntry("In OnStop.");
            LogControl.Debug("Come in OnStop");
        }

 里面有:Log4net 日志。查考另一篇文章

找到:C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools    点击运行

客户机器上安装:找到 对应的.net Framework安装位置,或者 用 cmd cd 到 安装位置例如:

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319  记得用管理员身份打开

安装服务:命令+地址:InstallUtil.exe D:\**\test\bin\Debug\test.exe

运行服务:命令+服务名称(serviceInstaller1:DisplayName:(HkcServer))

:net start HkcServer

停止服务:命令+服务名称(serviceInstaller1:DisplayName:(HkcServer))

:net stop HkcServer

卸载服务:命令+地址:InstallUtil.exe /u D:\**\test\bin\Debug\test.exe

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值