C#程序启动起来之后,等待C++发送操作。
在C++程序,按一次回车,C#程序执行一次循环
。。。。。。
C++
#include <windows.h>
#include <stdio.h>
const WCHAR mutextName[] = L"five_exe_mutex_sync";
HANDLE g_hMutex;
int main()
{
g_hMutex = CreateMutex(NULL, FALSE, mutextName);
while (true)
{
WaitForSingleObject(g_hMutex, INFINITE);
printf("按回车,继续");
getchar();
ReleaseMutex(g_hMutex);
}
CloseHandle(g_hMutex);
}
C#
string MutexName = "five_exe_mutex_sync";
Mutex? mutex;
OpenMutex();
while (true)
{
if (mutex.SafeWaitHandle.IsClosed == false && mutex.SafeWaitHandle.IsInvalid == false)
{
Console.WriteLine("waite......");
try
{
mutex.WaitOne();
Console.WriteLine("获取互斥访问权! 访问资源完毕");
mutex.ReleaseMutex();
mutex.Close();
}
catch { }
}
else
{
OpenMutex();
}
};
void OpenMutex()
{
while (Mutex.TryOpenExisting(MutexName, out mutex) == false)
{
Console.WriteLine("waite...mutex...");
Thread.Sleep(500);
}
}