您的位置:首页 > 运维架构

建立一个windows服务(可用于实现计划任务,事件监控..) .NET

2013-05-02 15:00 786 查看
什么是windows服务? 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序。Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。这些启动方式包括了自动启动和手动启动两种。对于自动启动的Windows服务程序,它们在 Windows启动或是重启之后用户登录之前就开始执行了。只要你将相应的Windows服务程序注册到服务控制管理器(Service Control Manager)中,并将其启动类别设为自动启动就行了。而对于手动启动的Windows服务程序,你可以通过命令行工具的NET START 命令来启动它,或是通过控制面板中管理工具下的服务一项来启动相应的Windows服务程序(见图1)。同样,一个Windows服务程序也不能像一般的应用程序那样被终止。因为Windows服务程序一般是没有用户界面的,所以你也要通过命令行工具或是下面图中的工具来停止它,或是在系统关闭时使得 Windows服务程序自动停止。因为Windows服务程序没有用户界面,所以基于用户界面的API函数对其是没有多大的意义。为了能使一个 Windows服务程序能够正常并有效的在系统环境下工作,程序员必须实现一系列的方法来完成其服务功能。Windows服务程序的应用范围很广,典型的 Windows服务程序包含了硬件控制、应用程序监视、系统级应用、诊断、报告、Web和文件系统服务等功能。 通过时间控件 定时检测实现执行任务的

自动会在相应的工程(如WinService1)中创建一个Service1.cs的文件(将Service1.cs改名为ScheduleService.cs),选中该文件,查看代码,

using System;
using System.ServiceProcess;
using System.Timers;

namespace WinService1
{
public partial class ScheduleService : ServiceBase
{
#region 属性定义

/// <summary>
/// 定时器
/// </summary>
Timer Timer1;

#endregion

public ScheduleService()
{
InitializeComponent();
Timer1 = new Timer();
ServiceName = "ScheduleService";
CanStop = true;
CanShutdown = true;
CanPauseAndContinue = true;
AutoLog = true;

Timer1.Elapsed += Timer1_Elapsed;
Timer1.Interval = 1000;
Timer1.Start();
}

/// <summary>
/// 达到间隔时间
/// </summary>
void Timer1_Elapsed(object sender, ElapsedEventArgs e)
{
if (e.SignalTime.Second == 0)//每分钟执行一次
{
//检测配置文件中,是否有需要执行的任务

if (false)
{
WriteLog("文件1", "已经检测过了,有要执行的任务:***,并已安排执行");
}
else
WriteLog("文件1", "已经检测过了,没有要执行的任务");
}
}

protected override void OnStart(string[] args)
{
base.OnStart(args);
WriteLog("文件1", "WinService1.OnStart" + " 服务启动");
}

protected override void OnStop()
{
base.OnStop();
WriteLog("文件1", "WinService1.OnStop" + " 服务停止");
}

#region Log

private static string logPath = string.Empty;
/// <summary>
/// 保存日志的文件夹
/// </summary>
public static string LogPath
{
get
{
if (logPath == string.Empty)
{
if (System.Web.HttpContext.Current == null)
// Windows Forms 应用
logPath = AppDomain.CurrentDomain.BaseDirectory;
else
// Web 应用
logPath = AppDomain.CurrentDomain.BaseDirectory + @"bin";
}
return logPath;
}
set { logPath = value; }
}

/// <summary>
/// 写日志
/// </summary>
/// <param name="logFile">文件名,如:Info</param>
/// <param name="msg">日志内容</param>
public static void WriteLog(string logFile, string msg)
{
try
{
System.IO.StreamWriter sw = System.IO.File.AppendText(
LogPath + logFile + " " +
DateTime.Now.ToString("yyyyMMdd") + ".Log"
);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
sw.Close();
}
catch
{ }
}

#endregion

}
}


新建类MyInstaller.cs

using System;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;
namespace WinService1
{
[RunInstaller(true)]
public partial class MyInstaller : Installer
{
private ServiceInstaller sInstall;
private ServiceProcessInstaller sProcessInstall;
public MyInstaller()
{
sInstall = new ServiceInstaller();
sProcessInstall = new ServiceProcessInstaller();
sProcessInstall.Account = ServiceAccount.LocalSystem;
sInstall.StartType = ServiceStartMode.Automatic;
sInstall.ServiceName = "ScheduleService"; //这个服务名必须和步骤1中的服务名相同。
sInstall.DisplayName = "任务计划 服务";
sInstall.Description = "这是该任务计划服务的描述..";
Installers.Add(sInstall);
Installers.Add(sProcessInstall);
}
}
}


在Program.cs中

using System.ServiceProcess;

namespace WinService1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ScheduleService()
};
ServiceBase.Run(ServicesToRun);

//在运行中,将当前目录转向WinService1.exe所在的目录,(盘符直接切换, cd 切换文件夹)
//然后输入 F:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe WinService1.exe
//则在服务中可以看到新增了一项服务

//另外,删除服务的方法如下:
//在运行中,将当前目录转向WinService1.exe所在的目录,
//sc delete myservice (myservice为当前要删除的服务名),即可删除该服务
}
}
}


生成一下该工程,然后

根据Program.cs中的相关注释,在 管理>服务中添加上 新的服务,然后启动;

在调试>附加到进程 中





此时即可调试服务程序
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐