思路: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. Update DN with Procedure

    Update DN )) LANGUAGE SQL MODIFIES SQL DATA BEGIN -- Step 1 UPDATE DNRITM A SET (DNITTQTY, DNIREQTY) ...

  2. 双向BFS

    转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2)  快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...

  3. [改善Java代码]不要只替换一个类

    建议20: 不要只替换一个类 我们经常在系统中定义一个常量接口(或常量类),以囊括系统中所涉及的常量,从而简化代码,方便开发,在很多的开源项目中已采用了类似的方法,比如在Struts2中,org.ap ...

  4. android中IdleHandler的使用

    IdleHandler 在API上面的解释如下: public final void addIdleHandler (MessageQueue.IdleHandler handler) 向消息队列中添 ...

  5. poj1472[模拟题]

    Instant Complexity Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2017   Accepted: 698 ...

  6. http keepalive

    转载自: http://www.92csz.com/17/1152.html http keepalive 在http早期 ,每个http请求都要求打开一个tpc socket连接,并且使用一次之后就 ...

  7. Entity Framework + WCF REST JSON Service

    利用EF 和WCF 建立一个REST JSON Service. 首先我们要下载一个Visual Studio 的Template 叫 "ADO.NET C# POCO Entity Gen ...

  8. HDOJ2000ASCII码排序

    ASCII码排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. SQL Server 2008 错误15023:当前数据库中已存在用户或角色

    解决SQL Server 2008 错误15023:当前数据库中已存在用户或角色,SQLServer2008,错误15023,在使用SQL Server 2008时,我们经常会遇到一个情况:需要把一台 ...

  10. 北大ACM(POJ1004-Financial Management)

    Question:http://poj.org/problem?id=1004问题点:求平均值及格式化输出. Memory: 248K Time: 0MS Language: C++ Result: ...