最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

C# 如何创建一个Windows服务(Windows Service)

业界 admin 8浏览 0评论

Windows服务经常用来做一些定时任务处理,今天来说一下如何搭建一个Windows服务(基础篇,不喜勿喷)。

1、搭建一个Windows Servier,我是VS2017 .NET FrameWork4.5.2的框架。

2、进入Service设计界面,右击鼠标弹出菜单,选中“添加安装程序”。


3、点击“serviceInstaller1”,在“属性”窗体将ServiceName改为MyService,Description改为测试,DisplayName改为Windows服务自动测试,StartType保持为Automatic


4、点击“serviceProcessInstaller1”,在“属性”窗体将Account改为 LocalService


5、选择工具 --》选择工具拖项 --》 Timer。


6、选择工具箱,拖动到“Service.cs”设计界面.


7、点击时间控件,写入自己业务代码。

using System;
using System.ServiceProcess;
using System.Threading;

namespace MyService
{
    public partial class Service1 : ServiceBase
    {
        private Thread thdStart;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            thdStart = new Thread(new ThreadStart(timer1.Start));
            thdStart.Start();
        }

        protected override void OnStop()
        {
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.timer1.Stop();
            string filePath = @"F:\Demo\MyService\MyService\测试\test.log";
            string strCont = "打印时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            System.IO.File.AppendAllText(filePath, strCont);
            this.timer1.Start();
        }
    }
}

8、生成项目解决方案,打开项目目录到Debug目录下面,新建两个install.batuninstall.bat批处理文件(直接建.txt文件再改后缀即可)。
install.bat:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /i MyService.exe
uninstall.bat:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /u MyService.exe

9、如果运行出现“InstallUtil.InstallLog”日志中出现如下图,请看我第一篇。


按照第一篇操作,我的结果在这里啦:

不会写代码的程序猿初次写博客,有问题请大家多多指教,谢谢。

Windows服务经常用来做一些定时任务处理,今天来说一下如何搭建一个Windows服务(基础篇,不喜勿喷)。

1、搭建一个Windows Servier,我是VS2017 .NET FrameWork4.5.2的框架。

2、进入Service设计界面,右击鼠标弹出菜单,选中“添加安装程序”。


3、点击“serviceInstaller1”,在“属性”窗体将ServiceName改为MyService,Description改为测试,DisplayName改为Windows服务自动测试,StartType保持为Automatic


4、点击“serviceProcessInstaller1”,在“属性”窗体将Account改为 LocalService


5、选择工具 --》选择工具拖项 --》 Timer。


6、选择工具箱,拖动到“Service.cs”设计界面.


7、点击时间控件,写入自己业务代码。

using System;
using System.ServiceProcess;
using System.Threading;

namespace MyService
{
    public partial class Service1 : ServiceBase
    {
        private Thread thdStart;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            thdStart = new Thread(new ThreadStart(timer1.Start));
            thdStart.Start();
        }

        protected override void OnStop()
        {
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.timer1.Stop();
            string filePath = @"F:\Demo\MyService\MyService\测试\test.log";
            string strCont = "打印时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            System.IO.File.AppendAllText(filePath, strCont);
            this.timer1.Start();
        }
    }
}

8、生成项目解决方案,打开项目目录到Debug目录下面,新建两个install.batuninstall.bat批处理文件(直接建.txt文件再改后缀即可)。
install.bat:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /i MyService.exe
uninstall.bat:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /u MyService.exe

9、如果运行出现“InstallUtil.InstallLog”日志中出现如下图,请看我第一篇。


按照第一篇操作,我的结果在这里啦:

不会写代码的程序猿初次写博客,有问题请大家多多指教,谢谢。

发布评论

评论列表 (0)

  1. 暂无评论