Visual Studio C ++ / CLR-(Mutex)无法将参数3从'bool *'转换为'bool%'

问题描述 投票:-1回答:2

我尝试在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函数吗,我该如何处理第三个参数。

c++-cli
2个回答
1
投票

您不需要&“ address of”运算符即可将bool转换为bool%

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

[其他说明:

  • 如果您不局限于检查createdNew,情况会更好。如果不是新创建的,则还需要尝试将其锁定。
  • 如果您发布Mutex,而不仅仅是关闭对它的引用并使其删除,那么效果会更好。您可以通过在呼叫ReleaseMutex之后将呼叫保留到ReleaseMutex来实现。
  • 您应该选择比“ MyApplicaitonName”更独特的名称。生成一个Guid,在键盘上敲击,类似这样。

0
投票

构造函数期望第三个参数为Application::Run。当您引用变量时,编译器将自动创建跟踪参考,因此只需删除tracking reference地址:

&

以上与分步等效相同:

bool createdNew;
System::Threading::Mutex^ mutex = gcnew System::Threading::Mutex(true, "MyName", createdNew);
© www.soinside.com 2019 - 2024. All rights reserved.