How to create an inherited semaphore in .NET?

I am trying to create a Win32 Semaphore object that is inherited. This means that any child processes that I start can automatically be allowed to act on the same Win32 object.

My code is as follows:

Semaphore semaphore = new Semaphore(0, 10);
Process process = Process.Start(pathToExecutable, arguments);

But the semaphore object in this code cannot be used by the child process.


The code I'm writing is a port of arrivals working in C ++. Old C ++ code provides this as follows:

SECURITY_ATTRIBUTES security = {0};
security.nLength = sizeof(security);
security.bInheritHandle = TRUE;
HANDLE semaphore = CreateSemaphore(&security, 0, LONG_MAX, NULL);

Then later, when CreateProcesscalled, the argument bInheritHandlesmatters TRUE.

(In both cases, C # and C ++, I use the same child process (which is C ++). It takes the semaphore identifier on the command line and uses the value directly when called ReleaseSemaphore.)


, SemaphoreSecurity ProcessStartInfo, .

+3
2

, ++, , P/Invoke ( ++/CLI).

+1

PInvoke CreateSemaphore ReleaseSemaphore, , CreateSemaphore.

0

Source: https://habr.com/ru/post/1738884/


All Articles