思路: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. UE设置 去掉bak备份文件

    使用ue打开文件,修改保存后,会产生.bak备份文件,感觉不爽,如何去掉呢? 1:在ue菜单栏,选择“高级”按钮选项  —— “配置”选项 2:在弹出的选择框中,找到“备份”—— 勾选“不备份” 选项 ...

  2. JS中获取table节点的tr或td的内容

    <table id="tb1" width="200" border="1" cellpadding="4" ce ...

  3. Android编程心得-ListView的Item高亮显示的办法

    在我们使用ListView的时候,经常会遇到某一项(Item)需要高亮显示的情况,如下图,有人说当我们点击子项的时候会变亮,但有时候业务逻辑需要让ITEM根据条件自动变亮,下面我来介绍一下我自己的解决 ...

  4. hdu 1170 Balloon Comes!

    Balloon Comes! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. javascript基础知识--函数定义

    函数声明式 function funname( 参数 ){ ...执行的代码 } 声明式的函数并不会马上执行,需要我们调用才会执行:funname(); * 分号是用来分隔可执行JavaScript语 ...

  6. stop()方法的精准应用

    stop()方法在动画中扮演了很精彩的角色,他能够阻止连续动画或连续事件出现累积的状况,令动画有条不紊的进行. 1语法结构 stop([clearQueue],[gotoEnd]); 这两个参数都是可 ...

  7. Quartz Scheduler(2.2.1) - hello world

    简单示例 1. maven 依赖 <dependencies> <dependency> <groupId>org.quartz-scheduler</gro ...

  8. sublime text使用技巧

    常用快捷键 Ctrl + L  选择整行(按住-继续选择下行) Ctrl + KK  从光标处删除至行尾 Ctrl + Shift+K  删除整行 Ctrl + Shift+D  复制光标所在整行,插 ...

  9. Java I/O第二篇 之 (缓冲流 随机流 数组流 数据流)

    1:缓冲流 BufferedReader  BufferedWriter 具有较强的读写能力,能单独读写一行数据,能够减少对硬盘的访问次数. /** * 缓冲流 能够增强对问价数据的可读性,减少访问读 ...

  10. (转载)Nim游戏博弈(收集完全版)

    Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输.后来,在一份资料上看到,这种游戏称为“拈(Nim)”.据说,它源 ...