Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not find a part of the path" error while creating Mutex

I'm baffled by this, can someone tell me why, when I call:

using (Mutex mtx = new Mutex(false, strId))
{
}

I get this exception:

Could not find a part of the path.

If strId is set to something like localhost\SQLEXPRESS-MyName-2?

like image 527
ahmd0 Avatar asked Dec 21 '13 00:12

ahmd0


1 Answers

From the docs:

On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

Because you are using a backslash in your name (\) it assumes you are trying to specify a visibility level, and then discovers localhost isn't a valid visibility level - hence generating the exception.

It's rather strange the .NET docs don't mention explicitly \ is a reserved character in Mutex names, the Win32 docs do explain it better (link):

The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session namespace. The remainder of the name can contain any character except the backslash character (). For more information, see Kernel Object Namespaces. Fast user switching is implemented using Terminal Services sessions. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.

like image 108
Niels Keurentjes Avatar answered Oct 12 '22 13:10

Niels Keurentjes