首页 > 系统相关 >c#开发和学习(c#编写windows服务)

c#开发和学习(c#编写windows服务)

时间:2022-11-23 15:03:42浏览次数:34  
标签:ServiceBase c# System windows WindowsService1 编写 using net Service1


        大家有没有想过一些程序,属于那种开机即启动的。比如说web服务器程序,mysql程序等等。但是呢,这些程序本身又没有任何的console对话框,所以这个时候就要把他们编写成windows服务程序。window服务本身也是一个exe文件,中间的一部分函数功能需要做override处理,正是这些override的函数保证了这个service可以接受外界command的输入。

        那么就说c#如何编写服务程序。事实上,不仅仅是c#,vb、f#、c、c++都可以编写服务程序。只是c#编写起来比较简单一点。本次实验的环境是win11+vs2017。

1、创建windows服务工程,

c#开发和学习(c#编写windows服务)_windows服务

         首先,在创建工程的时候选择windows 服务即可。因为我们选择的是基础的.net framework,所以这里也做了相应的Windows Service选项。

2、确认Program.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WindowsService1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}

        这部分是服务的入口点,相当于exe的主函数main。其主要作用是把Service1加入到整个ServicesToRun中。内容比较简单。这部分代码不需要做任何修改。

3、第一个服务Service1.cs文件

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;

namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("D:\\info.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");
}
}

protected override void OnStop()
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("D:\\info.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");
}
}
}
}

        整个类继承自ServiceBase。结构上就是一个八股文,除了在构造函数中千篇一律调用InitializeComponent之外,剩下来的OnStart和OnStop就是用户自己需要添加内容的地方。OnStart对应net start命令,OnStop对应net stop命令。

        这里为了功能说明,只是在OnStart和OnStop做了一些字符保存的动作。不出意外,在Service启动和推出的时候,会在D:多一个info.txt文件出来。

4、编译

        和正常的项目编译一样,最终会生成一个可执行文件,即WindowsService1.exe。

5、注册服务

sc create WindowsService1 binpath="C:/Users/feixiaoxing/Desktop/WindowsService1/WindowsService1/bin/Debug/WindowsService1.exe" type=own start=auto displayname=WindowsService1

6、启动服务

net start WindowsService1

7、退出服务

net stop WindowsService1

8、确认d盘是否有文件生成

        不出意外的话,应该在d盘有info.txt生成,文件内容如下所示,

2022-09-25 16:20:55 Start.
2022-09-25 16:21:10 Stop.

9、解除服务注册

sc delete “WindowsService1”

标签:ServiceBase,c#,System,windows,WindowsService1,编写,using,net,Service1
From: https://blog.51cto.com/feixiaoxing/5881299

相关文章

  • opcv3.4.16 安装
    下载:链接:https://pan.baidu.com/s/1pE9WyrrSQHdceLq3-IqvDA提取码:3qgi解压:tar-xvfopencv-3.4.16_ippcv.tar.gz复制ippicv:cpopencv-3.4.16/Downloads/ippicv......
  • c#开发和学习(c#调用dll)
        c和c++作为早期的开发语言,积累了大量的可用库。后续的开发语言,虽然在易用性和容易程度上面有了很大的提高,但是对于曾经的开发库,是无法做到弃之不用的。因此,对于......
  • cpu设计和实现(取指)
        cpu设计的本质是数字电路的设计。要是没有verilog、vhdl这些语言,那么剩下来使用的方法基本只有卡诺图这一种了。在数字电路中,有两种基本的电路,一种是逻辑电路,一......
  • scrapy爬取后中文乱码,解决word转为html 时cp1252编码问题
    解决思路1、循环暴力寻找编码,但是不如思路3defparse(self,response):print(response.text[:100])body=response.body#直接是bytes,response.tex......
  • Codeforces Round #835 (Div.4) A-G题解
    原题链接:https://codeforces.com/contest/1744A.MediumNumber题意:给定三个数,求中间那个数(倒数第二小or倒数第二大)。题解:直接用数组存,sort一下输出中间的数即可。#in......
  • c#开发和学习(基础)
        原先对c#了解不多,后来进入非标领域,才知道原来大多数非标上位机软件都是用c#开发的。之前在研究所的时候,曾经想过用qt开发上位机软件,但是qt是基于c++语言的,这对......
  • 常用js库和框架(echarts)
    前端库,不管是饼图、柱状图,都可以很轻松的画出来。所需要的准备,就是把数据准备好就可以了。下面一个简单的例子来说明下,1、准备demo文件<head><metacharset="utf-8"/>......
  • Spring Cache + Redis;用Spring Cache的注解自动管理Redis缓存
    https://blog.csdn.net/qq_45839663/article/details/127209491?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHu......
  • composer 创建项目错误
    Problem1-league/flysystem[1.1.0,...,1.1.10]requireext-fileinfo*->itismissingfromyoursystem.InstallorenablePHP'sfileinfoextension.......
  • springcloud -nacos-配置中心-接入
    1.nacaosClient接入:pom.xml引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-actuator</artifactId>......