Topshelf C# 开发 Windows 服务程序最简单的方式
Topshelf 官方网站: http://topshelf-project.com/
Topshelf 文档地址: https://topshelf.readthedocs.io/en/latest/index.html
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Timers;
using Topshelf;
using System.Threading;
namespace TopshelfDemo
{
public class ServiceRunner : ServiceControl, ServiceSuspend
{
Thread th = null;
void ThreadMethod()
{
while (true)
{
Console.WriteLine("time is {0} ", DateTime.Now);
System.Threading.Thread.Sleep(1000);
}
}
public bool Continue(HostControl hostControl)
{
throw new NotImplementedException();
}
public bool Pause(HostControl hostControl)
{
throw new NotImplementedException();
}
public bool Start(HostControl hostControl)
{
th = new Thread(new ThreadStart(ThreadMethod));
th.Start();
return true;
}
public bool Stop(HostControl hostControl)
{
th.Abort();
return true;
}
}
public class Program
{
public static void Main()
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
HostFactory.Run(x =>
{
x.UseLog4Net();
x.Service<ServiceRunner>();
x.SetDescription("测试服务服务");
x.SetDisplayName("lxw_Service");
x.SetServiceName("lxw_Service");
x.EnablePauseAndContinue();
});
}
}
}
安装服务、启动服务

查看服务

停止、卸载

Topshelf C# 开发 Windows 服务程序最简单的方式的更多相关文章
- 关于开发Windows服务程序容易搞混的地方!
在开发Windows服务程序时,我们一般需要添加安装程序,即:serviceInstaller,里面有几个关于名称属性,你都搞明白了吗? 1.Description:表示服务说明(描述服务是干什么的) ...
- 开发WINDOWS服务程序
开发WINDOWS服务程序 开发步骤: 1.New->Other->Service Application 2.现在一个服务程序的框架已经搭起来了,打开Service1窗口,有几个属性说明 ...
- 开发Windows程序的三种方式
软件开发方式一共有三种:SDK方式.MFC开发方式.托管环境的开发都是基于消息的开发 SDK方式 原装api的调用SDK方式使用C语言和Windows应用程序编程接口(Windows API)来开发W ...
- vs下开发windows服务程序
一. VS2012下开发Windows服务 1. 打开VS2012,新建项目,选择Windows服务,此处我以开发一个定时自动发送邮件的服务来做介绍,如下图: 2. 创建好后,编译器会自动创建一些文件 ...
- C#开发Windows服务 附简单实例实现禁止QQ运行
本实例主要实现下面三个基本功能 1.C#开发windows服务 2.禁止QQ等程序运行 3.为windows服务创建自动安装程序 下面针对这三个基本功能进行实现 一.C#开发windows服务 Win ...
- Delphi开发Windows服务程序
开发步骤: 1.New->Other->Service Application 2.现在一个服务程序的框架已经搭起来了 打开Service1窗口,有几个属性说明一下: AllowPause ...
- topshelf 开发windows 服务资料
官方配置 http://docs.topshelf-project.com/en/latest/configuration/config_api.html#service-start-modes to ...
- C#开发Windows服务详细流程
1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...
- 使用Visual Studio 2015 Community 开发windows服务
昨天研究在.NET下开发Windows服务程序,期间遇到一些小问题,这里仅将自己的开发过程和需要注意的地方写下和广大网友分享…… 1.基础 Windows服务是指系统启动时能够自己运行的程序.W ...
- .net开发windows服务
最近一个月都异常的繁忙,项目进度非常的紧,回头看看自己的blog,整整一个5月都没有一篇文章,非常惭愧,现在补几篇文章,介绍一下我最近关注的技术.这篇文章将介绍Windows服务程序的开发.摘要:本文 ...
随机推荐
- mysql 8.0 配置文件my.cnf中文注解【转载】
########################################################################### ## my.cnf for MySQL 8.0. ...
- 基于 junit5 实现 junitperf 源码分析
前言 上一节介绍了基于 junit4 实现 junitperf,但是可以发现定义变量的方式依然不够优雅. 那可以让用户使用起来更加自然一些吗? 有的,junit5 为我们带来了更加强大的功能. 拓展阅 ...
- pyhotn3.8 apt机器基础镜像构建
FROM python:3.8.10-slim RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/source ...
- Linux驱动开发笔记(四):设备驱动介绍、熟悉杂项设备驱动和ubuntu开发杂项设备Demo
前言 驱动的开发需要先熟悉基本概念类型,本篇讲解linux杂项设备基础,还是基于虚拟机ubuntu去制作驱动,只需要虚拟机就可以尝试编写注册杂项设备的基本流程. linux三大设备驱动 字符设 ...
- Singularity容器
"""参考文档 https://apptainer.org/user-docs/master/build_a_container.html ""&qu ...
- 【MongoDB】MongoDB原理分析、集群搭建(Docker)与简单使用
一.MongoDB 简介 MongoDB是一个基于分布式文件存储的数据库,介于关系数据库和非关系数据库之间,是非关系数据库当中功能最丰富,最像关系数据库的.其目的是为WEB应用提供可扩展的高性能数据存 ...
- JS笔记:方法两次调用,执行不同分支(公共变量,闭包,类三种方法实现)
好家伙, 当我们对一个方法进行两次调用,我们希望第一次执行A分支,第二次执行B分支,该怎么做? 这意味着在连续的两次调用中,方法的执行逻辑会交替执行不同的分支. 方法一:公共变量 let flag ...
- Redis动态配制,限内存,免重启
p.p1 { margin: 0; font: 14px Menlo; color: rgba(0, 255, 255, 1); background-color: rgba(0, 0, 0, 0.8 ...
- 【Azure Redis】Redis导入备份文件(RDB)失败的原因
问题描述 在测试Azure Redis的导入/导出备份文件的功能中,突然发现在Redis 4.0上导入的时候,一直报错. 问题解答 因为门户上只是显示导入失败,没有任何错误消息说明.根据常理推断,Re ...
- HashMap很美好,但线程不安全怎么办?ConcurrentHashMap告诉你答案!
写在开头 在<耗时2天,写完HashMap>这篇文章中,我们提到关于HashMap线程不安全的问题,主要存在如下3点风险: 风险1: put的时候导致元素丢失:如两个线程同时put,且ke ...