我試圖調解一個小的Windows服務,使它在啓動過程中等待來自另一個進程的信號。當然,我知道這種方法可能(甚至會)會導致服務啓動超時。事實並非如此。Windows服務看不到命名的信號量
問題是我用於調解目的的命名System.Thread.Sempaphore。信號量是使用以下構造創建並獲得的。 GC沒有改變,因爲我爲了測試的目的明確地將執行權限分配到給定行之下。
Boolean newOne;
System.Threading.Semaphore rootSemaphore =
new System.Threading.Semaphore(1, 1, "DummyServiceSemaphore", out newOne);
上面的代碼很好,很明顯。在調試模式下或在控制檯應用程序在執行時下面的代碼工作很好:
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");
完全相同的代碼時,作爲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);
}
}
因此,在lookig求助這個。 PS:我一直在嘗試使用Mutex,但是還有另一個問題 - 當所有者調用Mutex.ReleaseMutex()時,等待應用程序沒有趕上。
UPDATE:
按阿努拉格Ranjhan響應我已經編輯信號燈創建日常如下與啄現在工作得很好:
Boolean newOne = false;
System.Security.Principal.SecurityIdentifier sid =
new System.Security.Principal.SecurityIdentifier(
System.Security.Principal.WellKnownSidType.WorldSid,
null);
System.Security.AccessControl.SemaphoreSecurity sec =
new System.Security.AccessControl.SemaphoreSecurity();
sec.AddAccessRule(new System.Security.AccessControl.SemaphoreAccessRule(
sid,
System.Security.AccessControl.SemaphoreRights.FullControl,
System.Security.AccessControl.AccessControlType.Allow));
System.Threading.Semaphore rootSemaphore =
new Semaphore(1, 1, "Global\\DummyServiceSemaphore", out newOne, sec);
謝謝!這解決了這個問題。 – Vitaly 2012-04-02 08:01:25
我最近遇到了同樣的問題,即使使用「全局」前綴,我仍然必須顯式指定SemaphoreSecurity對象,如上面問題更新中所示,以使其在Windows服務中工作。不過,這個工作! – mthierba 2015-01-13 14:14:30