可视化Windows服务定时任务
思路:1、新建一个《Windows服务项目》,在里面建服务文件(可以多个),用来做要执行的任务。
2、新建一个《Windows窗体应用程序》,在这个程序里管理服务。如:安装、卸载、启动、停止。
示例(定时写日志):
1、新建解决方案,如图:
2、LogService里新建服务文件(可以建多个,一个服务文件就是一个服务):
3、打开服务文件,右键:
4、设置属性:
5、在服务文件(WriteLog)里写要执行的任务:
using System;
using System.ServiceProcess;
using System.Timers; namespace LogService
{
partial class WriteLog : ServiceBase
{
private Timer timer = new Timer(); public WriteLog()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
//服务开启执行代码
timer.Enabled = true;
timer.Interval = ;
timer.Elapsed += timer_Elapsed; }
protected void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (DateTime.Now.Second == )
{
LogHelper.ErrorLog("这里是要做的事", "D:\\", "log.txt");
}
} protected override void OnStop()
{
//服务结束执行代码
timer.Enabled = false;
} protected override void OnPause()
{
//服务暂停执行代码
}
protected override void OnContinue()
{
//服务恢复执行代码
}
protected override void OnShutdown()
{
//系统即将关闭执行代码
}
}
}
6、服务的Program.cs里这样配置(可以配置多个服务文件):
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace LogService
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WriteLog()//这里可以配置多个服务文件
};
ServiceBase.Run(ServicesToRun);
}
}
}
7、服务制作完成,在应用程序里新建一窗体,用于对服务进行控制:
8、窗体源码:
using System;
using System.ServiceProcess;
using System.Windows.Forms; namespace LogServiceSetup
{
public partial class 定时任务 : Form
{
private string serviceExe = "LogService.exe"; private string serviceName = "WriteLog"; public 定时任务()
{
InitializeComponent(); if (ServiceAPI.ExistService(serviceName))
{
安装.Enabled = false; ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Stopped || service.Status == ServiceControllerStatus.Paused)
{
label1.Text = "服务未启动";
启动.Enabled = true;
卸载.Enabled = true;
停止.Enabled = false; }
if (service.Status == ServiceControllerStatus.Running)
{
label1.Text = "服务正在运行";
启动.Enabled = false;
停止.Enabled = true;
卸载.Enabled = false;
}
}
else
{
label1.Text = "服务未安装";
安装.Enabled = true;
卸载.Enabled = false;
启动.Enabled = false;
停止.Enabled = false;
}
} private void 安装_Click(object sender, EventArgs e)
{
try
{
ServiceAPI.Installter(serviceExe);
label1.Text = "服务已安装"; 安装.Enabled = false;
卸载.Enabled = true;
启动.Enabled = true;
停止.Enabled = false;
}
catch (Exception ex) { label1.Text = ex.Message; }
} private void 卸载_Click(object sender, EventArgs e)
{
try
{
ServiceAPI.Uninstall(serviceExe);
label1.Text = "服务已卸载"; 安装.Enabled = true;
卸载.Enabled = false;
启动.Enabled = false;
停止.Enabled = false;
}
catch (Exception ex) { label1.Text = ex.Message; }
} private void 启动_Click(object sender, EventArgs e)
{
try
{
ServiceAPI.Start(serviceName);
label1.Text = "服务已启动"; 安装.Enabled = false;
卸载.Enabled = false;
启动.Enabled = false;
停止.Enabled = true;
}
catch (Exception ex) { label1.Text = ex.Message; }
} private void 停止_Click(object sender, EventArgs e)
{
try
{
ServiceAPI.Stop(serviceName);
label1.Text = "服务已停止"; 安装.Enabled = false;
卸载.Enabled = true;
启动.Enabled = true;
停止.Enabled = false;
}
catch (Exception ex) { label1.Text = ex.Message; }
}
}
}
9、ServiceAPI源码:
using System;
using System.Configuration.Install;
using System.Reflection;
using System.ServiceProcess; namespace LogServiceSetup
{
public class ServiceAPI
{
//服务是否存在
public static bool ExistService(string serviceName)
{
bool exist = false;
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
{
exist = true;
break;
}
}
return exist;
} // 安装服务
public static void Installter(string serviceExe)
{
try
{
string serviceFileName = Assembly.GetExecutingAssembly().Location;
string[] cmdline = { };
TransactedInstaller transactedInstaller = new TransactedInstaller();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceExe, cmdline);
transactedInstaller.Installers.Add(assemblyInstaller);
transactedInstaller.Install(new System.Collections.Hashtable());
}
catch (Exception ex)
{
throw ex;
}
} // 卸载服务
public static void Uninstall(string serviceExe)
{
try
{
string[] cmdline = { };
TransactedInstaller transactedInstaller = new TransactedInstaller();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceExe, cmdline);
transactedInstaller.Installers.Add(assemblyInstaller);
transactedInstaller.Uninstall(null);
}
catch (Exception ex)
{
throw ex;
}
} //启动服务
public static void Start(string serviceName)
{
ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Stopped || service.Status == ServiceControllerStatus.Paused)
{
service.Start();
try
{
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(, , ));
}
catch (Exception ex)
{
throw ex;
}
}
} //暂停服务
public static void Pause(string serviceName)
{
ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Running)
{
service.Pause();
try
{
service.WaitForStatus(ServiceControllerStatus.Paused, new TimeSpan(, , ));
}
catch (Exception ex)
{
throw ex;
}
service.Close();
}
} //恢复服务
public static void Continue(string serviceName)
{
ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Paused)
{
service.Continue();
try
{
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(, , ));
}
catch (Exception ex)
{
throw ex;
}
}
} //停止服务
public static void Stop(string serviceName)
{
ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
try
{
service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(, , ));
}
catch (Exception ex)
{
throw ex;
}
service.Close();
}
}
}
}
7、把服务生成的文件(\bin\debug\)copy到应用程序中(\bin\debug\)。
8、运行应用程序:
可视化Windows服务定时任务的更多相关文章
- windows服务定时任务
其实定时任务时不时会碰到,只不过解决方案也不是只有一个,网上也有很多文章,但是没有一篇说得很清楚,尤其是安装环节,今天就着重说一下安装, 其他步骤带过,C#开发windows服务,开发,安装,调试 1 ...
- windows服务 定时任务
1.c#程序做成windows服务 若用cmd安装: var path = Process.GetCurrentProcess().MainModule.FileName + " s&quo ...
- C#开发可以可视化操作的windows服务
使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一 直执行我们开发的某些程序的时候.我经常看到许多人开发的win ...
- 【C#】开发可以可视化操作的windows服务
使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一直执行我们开发的某些程序的时候.这里我以一个WCF的监听服务为 ...
- C#开发人员能够可视化操作windows服务
使用C#开发自己的定义windows服务是一个很简单的事.因此,当.我们需要发展自己windows它的服务.这是当我们需要有定期的计算机或运行某些程序的时候,我们开发.在这里,我有WCF监听案例,因为 ...
- Windows服务中用Timer和线程两种方式来执行定时任务
在Service服务文件夹下新建Windows服务 - TestService
- 定时任务-C#线程类 windows服务
原理 最常用的就是C#中 timer类写一个定时方法,然后在把他宿主到windows服务里面. C#中Timer分类 关于C# Timer类 在C#里关于定时器类就有3个 C# Timer使用的方法 ...
- C#开发Windows服务详细流程
1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...
- 不用写Windows服务实现定时器功能(FluentScheduler )
MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...
随机推荐
- Oracle 数据泵导入导出总结
Oracle 数据泵(IMPDP/EXPDP)导入导出总结 Oracle数据泵导入导出是日常工作中常用的基本技术之一,它相对传统的逻辑导入导出要高效,这种特性更适合数据库对象数量巨大的情形,因为我日常 ...
- Linux 源码安装apache 与常见错误解决
文档原位置 一.编译安装apache 1.解决依赖关系 httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级. 升级方式有两种,一种是通过源代码编译安装,一种是直接升 ...
- saltstack实战4--综合练习1
规范配置管理 实际工作中可能会有现网环境,基线环境,开发环境. 需要使用saltstack统一管理.机器多了,业务多了,可能配置文件存放的会比较乱,因此可以统一管理起来 我们可以再加2个目录,test ...
- Ubuntu声音消失
声音图标是 X 将sound的Mute的勾去掉就有声音了. 参考文献: Ubuntu声音全部消失 使用的工具: Shutter
- No Dialect mapping for JDBC type: -1
MySQL数据库中有张表的字段是text,查询出来后对应的java类型是String,Dialect设置为org.hibernate.dialect.MySQLDialect 运行的时候报错:No D ...
- 极其简单的使用基于gulp和sass前端工作流
简单的记录自己如何在实际工作中使用gulp和sass的.我的原则是,小而美! gulp与sass介绍 gulp 什么是gulp?和Grunt一样,是一种任务管理工具:和Grunt又不一样,gulp是一 ...
- css选择器有哪些?哪些属性可以继承?优先级算法如何计算?内联和important哪个优先
CSS选择器:基本可以分为通配选择器,标签选择器,类选择器,ID选择器,简单属性选择,具体属性选择,根据部分属性值选择,特定属性选择,从结构上来分还有后代选择器,子元素选择器,相邻兄弟选择器以及伪类. ...
- 解决MS Office下载网站数据失败的问题
最近遇到在MS Excel中建立的Web Query在创建完成后过了一段时间(或关闭文件后再次打开文件并刷新数据)出现无法刷新的问题,点击刷新时报错如下: 无法下载您要求的信息. 这是一个很不友好的报 ...
- javaweb 乱码总结
可能的错误地方: 1.jsp页面编码 2.表单编码 3.servlet可接受编码 4.tomcat中server.xml文件中的指定编码 所有的编码要统一,一般使用“UTF-8”比较好 我最近一次出错 ...
- 解决oracle归档日志写满了(ORA-00257)的问题
解决ORA-00257: archiver error. Connect internal only, until freed 此问题属于归档日志满了. 解决办法: SQL> select * ...