我正在尝试调解一个小的Windows服务,使其在启动期间等待来自另一个进程的信号.当然,我知道这种方法可能(甚至会)有时会导致服务启动超时.事实并非如此.
问题是名为System.Thread.Sempaphore我用于调解目的.使用以下构造在其他地方创建和获取信号量.没有更改GC有它,因为我明确地在给定的行下方执行测试以进行测试.
Boolean newOne;
System.Threading.Semaphore rootSemaphore =
new System.Threading.Semaphore(1, 1, "DummyServiceSemaphore", out newOne);
Run Code Online (Sandbox Code Playgroud)
上面的代码显然效果很好.以下代码在调试模式或控制台应用程序下执行时效果很好:
Boolean createdNew;
System.Threading.Semaphore semaphore =
new System.Threading.Semaphore(1, 1, "DummyServiceSemaphore", out createdNew);
if (createdNew)
throw new Exception("That's not what we wanted");
Run Code Online (Sandbox Code Playgroud)
作为Windows服务的一部分执行时,完全相同的代码失败:
static class Program
{
static void Main(string[] args)
{
Boolean createdNew;
System.Threading.Semaphore semaphore =
new System.Threading.Semaphore(1, 1, "DummyServiceSemaphore", out createdNew);
if (createdNew)
throw new Exception("That's not what we wanted");
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new Dummy() };
ServiceBase.Run(ServicesToRun);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,请寻求帮助.
PS:我一直在尝试使用Mutex,但是还有另外一个问题 - …