您的位置:首页 > 其它

手把手教你制作一个Windows服务

2012-03-06 16:32 232 查看

手把手教你制作一个Windows服务

也许你认为开发Windows服务是件非常困难的事件,需要了解非常多的操作系统原理以及精通Windows的各种API。其实没有这么夸张,Windows服务就像一个框架一样,至于你往里面加些什么完全在与你自己。好吧,现在我们就往里面加“Helloworld”吧。

开学习制作Windows服务之前我们先大概了解下它吧。Windows服务就是一些在后台运行的应用程序,这些程序都托管到Windows操作了。服务有几个常用的操作:启动,停止,禁用。将这些操作其实就是:运行一个应用程序,关掉一个应用程序,把应用程序扔到一个我们不关注的地方。
废话不多说了,我们现在开始制作吧。具体操作间下图:



图1



图2



图3



图4



图5



图6



图7

在“MyHelloworldService.cs”文件中输入如下代码:

[csharp]
view plaincopyprint?

using System;
using System.Collections.Generic;

using System.ComponentModel;

using System.Data;
using System.Diagnostics;

using System.Linq;
using System.ServiceProcess;

using System.Text;

using System.IO;

namespace MyHelloworldService

{
public partial class MyHelloworldService : ServiceBase

{
private static
bool isOk = false;

private static
object sync = new
object();
public MyHelloworldService()

{
InitializeComponent();
System.Timers.Timer looper =
new System.Timers.Timer(1000);
looper.Elapsed += (o, e) =>
{
if (isOk)

{
lock (sync)

{
WriteHelloWorld();
}
}
};
looper.Start();

}

protected override
void OnStart(string[] args)

{

isOk = true;

}

protected override
void OnStop()
{
isOk = false;
}

private void WriteHelloWorld()

{
string filePath = @"c:\myhellowrold.txt";

FileStream stream = null;

StreamWriter writer = null;

if (!File.Exists(filePath))

{
stream = File.Create(filePath);
}

stream = File.Open(filePath, FileMode.Append);
writer = new StreamWriter(stream);

writer.WriteLine("HelloWorld--" + DateTime.Now.ToString());

writer.Flush();

writer.Close();
stream.Close();
}
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

using System.IO;

namespace MyHelloworldService
{
public partial class MyHelloworldService : ServiceBase
{
private static bool isOk = false;
private static object sync = new object();
public MyHelloworldService()
{
InitializeComponent();
System.Timers.Timer looper = new System.Timers.Timer(1000);
looper.Elapsed += (o, e) =>
{
if (isOk)
{
lock (sync)
{
WriteHelloWorld();
}
}
};
looper.Start();

}

protected override void OnStart(string[] args)
{

isOk = true;

}

protected override void OnStop()
{
isOk = false;
}

private void WriteHelloWorld()
{
string filePath = @"c:\myhellowrold.txt";
FileStream stream = null;
StreamWriter writer = null;

if (!File.Exists(filePath))
{
stream = File.Create(filePath);
}

stream = File.Open(filePath, FileMode.Append);
writer = new StreamWriter(stream);

writer.WriteLine("HelloWorld--" + DateTime.Now.ToString());
writer.Flush();

writer.Close();
stream.Close();
}
}
}

简介:
上面中的“onstart”,“onstop”事件是在服务“启动”和“关闭”时触发的。

服务安装部分

至此我们已经将一个简单的服务开发完成了,接下来我们将要把它安装Windows操作系统上。首先我们需要安装服务的安装程序“InstallUtil.exe”,该程序可以在目录“C:\Windows\Microsoft.NET\Framework\v4.0.50727”中获取(注意:InstallUtil.exe工具如果是基于.Net
Frameworks 2.0,那么就只能注册基于相同框架下编写的Windows服务)。
如果该目录不存在也可以用“Installutil”作为关键字对C盘(操作系统的安装盘符)进行搜索。然后将文件“InstallUtil.exe”和服务生成文“MyHellworldService.exe”文件拷贝到同一文件夹下,具体操作见下图:



图8



图9



图10



图11

接下来就要通过”命令行提示符”来注册我们的服务了。通过组合键“Ctrl+R”调出“运行窗口”并在窗口里面输入“CMD”,就能调出“命令提示符了”。在提示符中输入如下命令:
命令一:

C:
命令二:
CD “C:\Users\GhostBear\Desktop\MyHelloworldService”
命令三:
installutil -i MyHelloworldService.exe

具体情况如下图:



图12



图13



图14

代码下载

下载本章的Demo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐