Windows Service 服务

创建Windows Service 项目

  1. 创建一个Windows Service项目,并将项目名称改为 TaskWindowService
  2. 在解决方案资源管理器内将Service1.cs改为TaskService.cs
  3. 在服务启动和结束时,记录日志
protected override void OnStart(string[] args)
{
LogHelper.Log("服务启动!");
} protected override void OnStop()
{
LogHelper.Log("服务停止!");
}
  1. 打开TaskService的设计界面,右键“添加安装程序”
  2. 此时软件会生成两个组件,分别为serviceInstaller1serviceProcessInstaller1
  3. 点击serviceInstaller1,在“属性”窗体将ServiceName改为TaskServiceDescription改为我的服务,StartType保持为Manual

  4. 点击serviceProcessInstaller1,在“属性”窗体将Account改为LocalSystem(服务属性系统级别)

  5. 鼠标右键点击项目TaskWindowService,在弹出的上下文菜单中选择“生成”按钮,到此WindowService项目搭建完毕

创建安装、启动、停止、卸载服务的Windows窗体

  1. 新建一个Windows Form项目,并命名为WindowsServiceClient
  2. 添加引用将TaskWindowService添加到WindowsServiceClient中,并将其设置为“启动项目”
  3. 创建安装、启动、停止、卸载 按钮并创建点击事件
string serviceFilePath = $"{Application.StartupPath}\\TaskWindowsService.exe";
string serviceName = "TaskService"; //事件:安装服务
private void button1_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
this.InstallService(serviceFilePath);
} //事件:启动服务
private void button2_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
} //事件:停止服务
private void button4_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
} //事件:卸载服务
private void button3_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
} //判断服务是否存在
private bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
} //安装服务
private void InstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
MessageBox.Show("服务安装成功!");
}
} //卸载服务
private void UninstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
MessageBox.Show("服务卸载成功!");
}
} //启动服务
private void ServiceStart(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
MessageBox.Show("服务启动成功!");
}
}
} //停止服务
private void ServiceStop(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
MessageBox.Show("服务停止成功!");
}
}
}

直接运行WindowsServiceClient项目,并点击“安装服务”会报错提示权限不足。

此时可以打开项目的Debug文件夹,找到WindowsServiceClient.exe文件,右键以管理员身份运行。

使用WIN+R的方式打开运行窗体,并在窗体内输入services.msc后打开服务,可以找到刚刚安装的TaskService

若需要直接运行项目来打开窗体安装服务,则需要使用UAC中Administrator的权限。在WindowsServiceClient项目中添加“应用程序清单文件”。

打开该文件将asInvoker修改为requireAdministrator

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

重启项目后就可以直接在VS中安装服务了。

以上内容主要转载自 https://www.cnblogs.com/cncc/p/7170951.html 然后做了一些小改动。

FluentScheduler 定时任务

关于FluentScheduler详细的介绍可以看下github的说明文档,介绍了所有的调用方式 https://github.com/fluentscheduler/FluentScheduler

  1. TaskWindowsService项目中通过nuget添加FluentScheduler

  2. TaskService中,服务启动的地方注册定时任务
protected override void OnStart(string[] args)
{
LogHelper.Log("服务启动!"); //每10秒执行一次任务
var registry = new Registry();
registry.Schedule(() =>
{
LogHelper.Log("服务运行中,执行定时任务!");
}).ToRunEvery(10).Seconds();
JobManager.Initialize(registry);
}

重新生成项目->运行->安装服务->运行服务,打开日志文件可以看到定时重新的任务的输出记录。

Windows Service 服务搭配FluentScheduler实现定时任务调度的更多相关文章

  1. quartz.net结合Topshelf实现windows service服务托管的作业调度框架

    topshelf可以很简单方便的实现windows service服务,详见我的一篇博客的介绍 http://www.cnblogs.com/xiaopotian/articles/5428361.h ...

  2. C# Windows Service服务的创建和调试

    前言 关于Windows服务创建和调试的文章在网络上的很多文章里面都有,直接拿过来贴在这里也不过仅仅是个记录,不会让人加深印象.所以本着能够更深刻了解服务项目的创建和调试过程及方法的目的,有了这篇记录 ...

  3. .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

    Windows Service(服务)  是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...

  4. 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……

    过场CG:   接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...

  5. C#制作Windows service服务系列二:演示一个定期执行的windows服务及调试(windows service)

    系列一: 制作一个可安装.可启动.可停止.可卸载的Windows service(downmoon原创) 系列二:演示一个定期执行的windows服务及调试(windows service)(down ...

  6. [开发笔记]-Windows Service服务相关注意事项

    注意一:报错:“本地计算机上的 *** 服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 该问题主要的原因是 Service服务程序中有错误. 遇到这个问题时,无论是重新安装服务,还是 ...

  7. war包部署在tomcat下,使用windows service服务方式启动tomcat服务器,在包含调用dll的模块,报dll找不到问题的解决办法

    问题描述: 开发了一个需要调用dll的java web程序,在idea开发环境下运行调试没问题,可以正常运行,在tomcat/bin下,运行批处理startup.bat,启动tomcat服务器,也可以 ...

  8. [开发笔记]-控制Windows Service服务运行

    用代码实现动态控制Service服务运行状态. 效果图: 代码: #region 启动服务 /// <summary> /// 启动服务 /// </summary> /// ...

  9. 使用C#编程语言开发Windows Service服务

    转载-https://www.cnblogs.com/yubao/p/8443455.html Create Windows Service project using Visual Studio C ...

随机推荐

  1. E203 itcm

    E203 itcm是64Kb,所以地址总线为16位,2^16=64Kb, 数据线宽度为64 bits(8 bytes),所以address width是16-3=13bit,ram depth 是2^ ...

  2. Java 方法引用_特性

    JAVA8 方法引用:(四种方法引用的使用) 对象引用的特点:不同的对象可以操作同一块的内容:而方法引用就是指为一个方法设置别名,相当于一个方法定义了不同的名字. 引用静态方法: 类名称 :: sta ...

  3. linux rz sz文件传输 ZModem协议

    比ftp和scp方便点.需要用支持ZModem协议的工具,SecureCRT是可以的 rz: 接收文件 sz: 发送文件 安装 # sudo apt-get install lrzsz 使用 协议介绍 ...

  4. 彻底解决Python3写爬虫或网站时的乱码问题

    第一次写贴子,试试水 很多玩Python3的都会遇到编码问题,如果直接去处理未知编码的网页,不是Python内部编码格式utf8会出现乱码,下面介绍一种将未知编码的字串转换为utf8来避免乱码的方法, ...

  5. wamp环境下composer及laravel的安装配置

    laravel: PHP Web开发框架 composer: PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 一.composer安装 参考:Windows ...

  6. hive的metatable学习

    一.存储Hive版本的元数据表(VERSION)   二.Hive数据库相关的元数据表(DBS.DATABASE_PARAMS) DBS --存储Hive中所有数据库的基本信息   DATAB ...

  7. 用python绘画一些简单图片

    python画笑脸 程序源代码 import turtle #画脸 t = turtle.Pen() t.speed(15) #t.circle(150) #t.color('orange') t.f ...

  8. MySQL数据库(一)索引

    索引的作用是操作数据库时避免全表扫描. 索引的机制 B Tree与B+Tree索引 B(blance) 树可以看作是对2-3查找树的一种扩展,即他允许每个节点有M-1个子节点. 根节点至少有两个子节点 ...

  9. Python—版本和环境的管理工具(Pipenv)

    pipenv简介 虚拟环境本质是一个文件,是为了适应不同的项目而存在.pipenv相当于virtualenv和pip的合体. 整合了 pip+virtualenv+Pipfile,能够自动处理好包的依 ...

  10. Windows文件夹共享和Unity的PersisterdataPath

    在共享机上存放unity开发的pc版本游戏,在其它机器双击就可以运行,但会遇到问题,比如: 游戏是需要下载资源的,默认情况下unity下载的资源是存放在persisterdataPath目录的,对于w ...