所以我有一个用 c# 编写的 Windows 服务。服务类派生自ServiceBase
,启动和停止服务分别调用实例方法OnStart
和OnStop
。这是该课程的SSCE:
partial class CometService : ServiceBase
{
private Server<Bla> server;
private ManualResetEvent mre;
public CometService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//starting the server takes a while, but we need to complete quickly
//here so let's spin off a thread so we can return pronto.
new Thread(() =>
{
try
{
server = new Server<Bla>();
}
finally
{
mre.Set()
}
})
{
IsBackground = false
}.Start();
}
protected override void OnStop()
{
//ensure start logic is completed before continuing
mre.WaitOne();
server.Stop();
}
}
可以看出,有很多逻辑要求当我们调用时OnStop
,我们正在处理与调用时相同的ServiceBase
实例OnStart
。
我可以确定是这种情况吗?