以下是我的代码:
...
private static void Main(string[] args)
{
string file=DateTime.Now.ToFileTime().ToString();
File.AppendAllText(file, "Mutex\r\n");
bool CreatedNew;
Mutex mutex=new Mutex(true, AppDomain.CurrentDomain.FriendlyName, out CreatedNew);
if(CreatedNew)
{
#if DEBUG
File.AppendAllText(file, "Launching in DEBUG mode\r\n");
#else
File.AppendAllText(file, "Launching in RELEASE mode\r\n");
#endif
//Program.Launch();
Program.ProcessArgsAndLaunch(args);
}
else
{
File.AppendAllText(file, "Handling dupe\r\n");
Program.HandleDuplicate();
}
}
...
我已经查看了许多文章,包括这里和其他网站,但都没有找到答案。
基本上,代码检查应用程序的运行实例,并在存在实例时切换到其主窗口。如果没有实例,则启动该应用程序。
在“Debug”模式下,一切都如预期般工作,问题出现在我将配置切换为“Release”时:应用程序总是启动(Mutex似乎什么也没做)。
我已经添加了有条件编译的转储,显示应用程序启动的模式,并且输出根据配置而改变,但不幸的是,应用程序的行为也是如此。
这可能是一个竞争条件,但我不确定。
如果需要,将发布更多代码。
谢谢。