C++ cli Visual Studio C++/CLR-(互斥)无法将参数3从';布尔*';至';布尔%&x27;

C++ cli Visual Studio C++/CLR-(互斥)无法将参数3从';布尔*';至';布尔%&x27;,c++-cli,C++ Cli,我尝试在clr互斥体中使用地址操作,因为我正在使用winforms进行开发,我无法真正理解%运算符在布尔变量声明中使用了什么Mutex(bool initiallyOwned,Syste::String^name,bool%createdNew)这是我正在使用的互斥函数的原型。由于参数的第三部分,我无法让它与我的实现一起工作。 bool createdNew = true; System::Threading::Mutex^ mutex = gcnew System::Threadin

我尝试在clr互斥体中使用地址操作,因为我正在使用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;
            }
    }
有人能帮助我如何调用互斥函数吗?更具体地说,我如何处理第三个参数。

构造函数希望第三个参数为。当您引用变量时,编译器将自动创建跟踪引用,因此只需删除以下地址的
&

bool createdNew;
System::Threading::Mutex^ mutex = gcnew System::Threading::Mutex(true, "MyName", createdNew);
上述内容与逐步等效内容相同:

bool createdNew;              // variable to be set by the mutex constructor
bool% trackNew = createdNew;  // explicit tracking reference to 'createdNew'
System::Threading::Mutex^ mutex = gcnew System::Threading::Mutex(true, "MyName", trackNew);


bool
转换为
bool%
不需要
&
操作员的“地址”

bool createdNew;
Mutex^ mutex = gcnew Mutex(true, "MyApplicationName", createdNew);
// Remove the ampersand ------------------ right here ^

其他说明:

  • 如果你不局限于检查
    createdNew
    ,事情会变得更好。如果它不是新建的,您还需要尝试锁定它
  • 如果您释放互斥锁,而不是仅仅关闭对互斥锁的引用并让它被删除,那么情况会更好。您可以通过在调用
    Application::Run
    之后继续调用来实现这一点
  • 你应该选择一个比“MyApplicationName”更独特的名字。生成Guid,敲击键盘,诸如此类