我尝试在clr Mutex中使用地址操作,因为我正在使用Winforms开发,所以我真的不明白%运算符在布尔变量声明中使用了什么。 Mutex(bool initiallyOwned, Syste::String ^name, bool %createdNew)
这是我正在使用的互斥锁函数的原型。由于参数的第三部分,我无法使其与我的实现配合使用。
bool createdNew = true;
System::Threading::Mutex^ mutex = gcnew System::Threading::Mutex(true, "MyApplicationName", &createdNew);
if (createdNew){
MessageBox::Show("First Instance");
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew ProjectMod::loginForm());
}else{
System::Diagnostics::Process^ current = System::Diagnostics::Process::GetCurrentProcess();
for each (System::Diagnostics::Process ^ process in System::Diagnostics::Process::GetProcessesByName(current->ProcessName))
if (process->Id != current->Id){
if (IsIconic((HWND)process->MainWindowHandle.ToPointer()))
ShowWindow((HWND)process->MainWindowHandle.ToPointer(), SW_RESTORE);
SetForegroundWindow((HWND)process->MainWindowHandle.ToPointer());
break;
}
}
有人可以帮助我如何更具体地调用Mutex函数吗,我该如何处理第三个参数。
您不需要&
“ address of”运算符即可将bool
转换为bool%
。
bool createdNew;
Mutex^ mutex = gcnew Mutex(true, "MyApplicationName", createdNew);
// Remove the ampersand ------------------ right here ^
[其他说明:
createdNew
,情况会更好。如果不是新创建的,则还需要尝试将其锁定。 ReleaseMutex
之后将呼叫保留到ReleaseMutex
来实现。 构造函数期望第三个参数为Application::Run
。当您引用变量时,编译器将自动创建跟踪参考,因此只需删除tracking reference地址:
&
以上与分步等效相同:
bool createdNew;
System::Threading::Mutex^ mutex = gcnew System::Threading::Mutex(true, "MyName", createdNew);