您说您的目标是检查一个外部应用程序是否正在运行(通过使用一个命名的互斥体)。对于这种情况,您不应该尝试在应用程序中创建具有给定名称的互斥对象,而应该只尝试打开这样的对象。原因很简单,如果外部应用程序使用这种互斥来检查它是否自己运行,那么实际上
这个互斥对象用于应用程序,而这个外部互斥对象永远不会启动。
TryOpenExisting
using System;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Mutex mutex;
if (Mutex.TryOpenExisting("My unique mutex name", out mutex)) {
try {
// with the used TryOpenExisting overload you can work with
// the mutex object here; you can wait for it or release
Console.WriteLine("Application is running!");
}
finally {
mutex.Close();
}
}
else {
Console.WriteLine("Application is NOT running!");
}
Console.ReadLine();
}
}
}