思路: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服务定时任务的更多相关文章

  1. windows服务定时任务

    其实定时任务时不时会碰到,只不过解决方案也不是只有一个,网上也有很多文章,但是没有一篇说得很清楚,尤其是安装环节,今天就着重说一下安装, 其他步骤带过,C#开发windows服务,开发,安装,调试 1 ...

  2. windows服务 定时任务

    1.c#程序做成windows服务 若用cmd安装: var path = Process.GetCurrentProcess().MainModule.FileName + " s&quo ...

  3. C#开发可以可视化操作的windows服务

    使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一 直执行我们开发的某些程序的时候.我经常看到许多人开发的win ...

  4. 【C#】开发可以可视化操作的windows服务

    使用C#开发自定义windows服务是一件十分简单的事.那么什么时候,我们需要自己开发windows服务呢,就是当我们需要计算机定期或者一直执行我们开发的某些程序的时候.这里我以一个WCF的监听服务为 ...

  5. C#开发人员能够可视化操作windows服务

    使用C#开发自己的定义windows服务是一个很简单的事.因此,当.我们需要发展自己windows它的服务.这是当我们需要有定期的计算机或运行某些程序的时候,我们开发.在这里,我有WCF监听案例,因为 ...

  6. Windows服务中用Timer和线程两种方式来执行定时任务

    在Service服务文件夹下新建Windows服务 - TestService

  7. 定时任务-C#线程类 windows服务

    原理 最常用的就是C#中 timer类写一个定时方法,然后在把他宿主到windows服务里面. C#中Timer分类 关于C# Timer类  在C#里关于定时器类就有3个 C# Timer使用的方法 ...

  8. C#开发Windows服务详细流程

    1.Windows服务简单介绍 Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务.一般情况下,用户不能通过用户界面来安装和启 ...

  9. 不用写Windows服务实现定时器功能(FluentScheduler )

    MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...

随机推荐

  1. [改善Java代码]覆写equals方法必须覆写hashCode方法

    覆写equals方法必须覆写hashCode方法,这条规则基本上每个Javaer都知道,这也是JDK API上反复说明的,不过为什么要这样做呢?这两个方法之间有什么关系呢?本建议就来解释该问题,我们先 ...

  2. RestEasy传值方式

    一.@pathparam    @PathParam 是一个参数注解,可以将一个 URL 上的参数映射到方法的参数上,它可以映射到方法参数的类型有基本类型.字符串.或者任何有一个字符串作为构造方法参数 ...

  3. CSS3—三角形

    话不多说看效果:演示效果,runjs 1.加了宽高和border,边用不同颜色显示,每条边都是一个梯形 2.去掉宽高,每条边都是三角形 3.只显示其中一条边就是不同的三角形了,是不是很简单,改变bor ...

  4. Jersey(1.19.1) - Client API, Security with Http(s)URLConnection

    With Http(s)URLConnection The support for security, specifically HTTP authentication and/or cookie m ...

  5. oracle中比较两表表结构差异和数据差异的方法

    在工作中需要完成这么一个需求:比较两个表的表结构是否形相同,并找出差异.比较两个表中的数据是否相同,并找出差异数据?    分析:由于表结构中字段比较多,手工比较很浪费时间,而且不能保证不出错误.对于 ...

  6. 最新app源码下载:200款优秀Android项目源码

    200款优秀Android项目源码!菜鸟必备!Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他 ...

  7. android组件间共享数据的常用方法

    使用Intent在激活组件的时候携带数据,以进行数据的传递 使用广播进行组件间数据的伟递 使用外部存储(sharedPreference,文件,数据库,网络)进行组件间数据共享 使用Static静态成 ...

  8. 第四十四篇、iOS开发中git添加.gitignore文件

    .gitignore文件可以直接使用https://github.com/github/gitignore 1.在项目中设置忽略文件(1)将从github上荡下来的对应的.gitignore文件(Sw ...

  9. System.Windows.Forms.Timer反编译学习

    using System; using System.ComponentModel; using System.Globalization; using System.Runtime; using S ...

  10. QMessageBox 在MAC下更加自然

    说明 在MAC写过QT程序的程序员应该都知道,QT默认的QMessageBox没有MAC系统的效果,在网上找到了一篇关于这方面的文章,但是这篇文章写的有个缺点,就是使用信号的方式,使用起来很不方便. ...