Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

c# - "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 localhostSQLEXPRESS-MyName-2?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...