MyApp1(C#)启动MyApp2(C#)。当MyApp2完全启动时,它会执行:
new Mutex(true, "MyApp2IsRunning");
与此同时,MyApp1一直在等待这种情况发生:
Mutex myApp2Mutex = null;
while (myApp2Mutex == null && !timedOut)
{
try
{
myApp2Mutex = Mutex.OpenExisting("MyApp2IsRunning");
}
catch(WaitHandleCannotBeOpenedException)
{
Thread.Sleep(100);
}
}
if (timedout) {return error;}
myApp2Mutex.WaitOne();
因此,如果MyApp2在规定的时间内启动,MyApp1现在会等待MyApp2IsRunning互斥锁,以了解用户何时退出MyApp2。
我只用C ++重写MyApp1。检测MyApp2状态的等效Mutex相关代码是什么?因此MyApp1仍将负责启动MyApp2,我仍然希望它能够检测MyApp2何时启动以及用户何时退出MyApp2。与上面相同的代码,但在C ++中。
答案 0 :(得分:0)
一个简单的谷歌搜索给你:
HANDLE mutex_you_want = CreateMutex(nullptr,true,“MyApp2IsRunning”);
答案 1 :(得分:0)
此伪代码使MyApp1能够等待MyApp2启动,然后检测用户何时退出MyApp2:
HANDLE hMutex = nullptr;
while (hMutex == nullptr && !timedout)
{
hMutex = OpenMutex(
SYNCHRONIZE,
FALSE,
"MyApp2IsRunning");
Sleep(1000);
}
if (timedout){return error;}
DWORD wait_result = WaitForSingleObject(hMutex, INFINITE);