参数计数与Windows窗体ShowDialog委托不匹配

时间:2011-12-13 02:36:00

标签: .net delegates c++-cli invoke

所以我只是试图从GUI线程的不同线程中显示一个表单。对于我的生活,我无法正确地为调用设置我的参数,这给了我来自catch的错误消息:

“参数计数不匹配”

我应该如何正确设置传入的参数?

#pragma once
#include "ErrorSystemStop.h"
using namespace System;
using namespace System::Windows::Forms;

delegate DialogResult ShowErrorWindow(System::Windows::Forms::IWin32Window ^ parentForm );

void ThrowErrorWindow(System::String^ strErrorMessage, int iNumberOfSegments, System::Windows::Forms::IWin32Window ^ parentForm)
{
//Only throw if we need too.
if(!bErrorPause)
{
    MainDisplay::ErrorSystemStop ^ stopMe = gcnew MainDisplay::ErrorSystemStop(strErrorMessage, iNumberOfSegments);
    ShowErrorWindow ^ disp = gcnew ShowErrorWindow(( System::Windows::Forms::Form ^)stopMe, &MainDisplay::ErrorSystemStop::ShowDialog);
    stopMe->TopMost = true;
    try
    {
        cli::array<System::Windows::Forms::IWin32Window ^> ^ Args = gcnew cli::array<System::Windows::Forms::IWin32Window ^>(1);
        Args[0] = parentForm;
        stopMe->Invoke(disp,( System::Windows::Forms::Form ^)stopMe, gcnew array<System::Object ^>{Args});
    }
    catch(Exception ^e)
    {
        e->Message;
    }
}
} //end ThrowErrorWindow

我也试过了:

array<Object^> ^ Args = {parentForm};
stopMe->Invoke(disp,(System::Windows::Forms::Form ^)stopMe, Args);

谢谢,

Alikar

1 个答案:

答案 0 :(得分:0)

所以事实证明我传递了三个参数,昨晚我真的很累,没注意到。

我已将代码浓缩为:

delegate void ShowErrorWindow(ErrorThrowClass ^ form, System::String^ strErrorMessage, int iNumberOfSegments );

void ThrowErrorWindow(System::String^ strErrorMessage, int iNumberOfSegments, System::Windows::Forms::Form ^ parentForm)
{
//Only throw if we need too.
if(!bErrorPause)
{
    ShowErrorWindow ^ disp = gcnew ShowErrorWindow(&ErrorThrowClass::LaunchErrorWindow);
    MthrControl(DataProtect,M_UNLOCK,M_DEFAULT);
    try
    {
        array<Object^> ^ Args = {parentForm, strErrorMessage, iNumberOfSegments};
        parentForm->Invoke(disp, Args);
    }
    catch(Exception ^e)
    {
        e->Message;
    }
    MthrControl(DataProtect,M_LOCK,M_DEFAULT);
}
} //end Throw ErrorWindow

启动其他错误窗口的代码现在是父GUI对象中的静态函数。这允许在一个线程中启动所有内容。