有时如果我只是想在Windows服务中测试一些代码而不需要它作为服务运行,我会做什么我将修改Main()。
所以不是我的主要是这样的:
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
我把它修改只是为了
Service1 service = new Service1();
service.Run();
// Put a breakpoint on the following line to always catch
// your service when it has finished its work
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
,并让说,我的OnStart方法看起来是这样的:
protected override void OnStart(string[] args)
{
// code to start your service.
ThreadStart ts = new ThreadStart(Run);
Thread t = new Thread(ts);
t.Start();
}
所以Run()只是你的OnStart方法启动的方法。
请记住,这不能保证您的服务作为服务安装后能够正常工作。但有时它对快速测试一些代码很有用。
来源
2011-04-06 16:17:44
Web
嗯,我想我必须这样做的第一种方法。 我没有运行(或类似的东西)在service1类... – RedEagle 2011-04-07 09:52:32
我添加了一个例子,我的意思与上面的Run()。 – Web 2011-04-07 11:11:27