Using a named system semaphore as an event to trigger something in another process.

Posted by jbraz on Stack Overflow See other posts from Stack Overflow or by jbraz
Published on 2010-06-11T03:48:35Z Indexed on 2010/06/11 3:53 UTC
Read the original article Hit count: 187

Filed under:
|

I have a thread that runs all the time:

private void DoSomeStuffThread() {
Semaphore sem = new Semaphore(0, 3, "sem_DoStuff");
sem.WaitOne();
do {
    //do some stuff
} while (sem.WaitOne());
}

I want to be able to only execute the stuff in the do block when something from another process says so. I am trying to use the "sem_DoStuff" named system semaphore to allow this to happen.

The code that gets executed in my other process:

public string DoStuff() {
try {
    Semaphore sem = Semaphore.OpenExisting("sem_DoStuff");
    sem.Release();
} catch (Exception e) {
    return e.Message;
}
}

So, the idea is that when DoStuff gets called, the semaphore gets released, and DoSomeStuffThread stops waiting, executes what is in the do block, and then waits for DoStuff again before it is getting called. But, when DoStuff gets called, I'm getting the exception 'No handle of the given name exists.'. What am I doing wrong?

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about semaphore