I'm trying to mediate a small windows service, making it to wait for a signal from another process during startup. For sure I know that such approach may (or even will) sometimes lead to service startup timeout. That's not the case.

The problem is with named System.Thread.Sempaphore I use for mediation purposes. Semaphore is created and acquired somewhere else using following construct. There's no change GC has it as I explicitly break the execution right below given line for test purposes.

Boolean newOne;
System.Threading.Semaphore rootSemaphore = 
    new System.Threading.Semaphore(1, 1, "DummyServiceSemaphore", out newOne);

Code above works well, obviously. Following code works well when being executed in debug mode or under console application:

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");

Exactly the same code fails when being executed as a part of Windows service:

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);
    }
}

Why the failure?

I've been trying to use Mutex instead, however there was another issue with it - waiting application doesn't catch up when owner calls Mutex.ReleaseMutex();


UPDATE:

As per Anurag Ranjhan response I've edited semaphore creating routine as follows and the thingy now

Edit
Report