suriel
suriel

Reputation: 337

Winform-Dialog in a .net-Lib called from native App

I want to open a WinForms-Dialog from a .net Classlibraries method that is called from a native Win32-App. The Win32-App is a MFC-App that uses Com

Win32:

void StartFlowControl(tagThreadPar* par) {
    //AfxOleInit is called before and succeeds
    //Load the C++/CLI-Dll that calls into the .net-Classlib
    HINSTANCE hModuleToNetBridge = AfxLoadLibrary(_T("ToNetBridge.dll"));
    typedef void(*RunFlowControlSignature)();
    auto runFlowControlMethod = (RunFlowControlSignature)GetProcAddress(hModuleToNetBridge, "RunFlowControl");
    runFlowControlMethod();
}

the C++/CLI-Dll

void RunFlowControl()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    gcroot<FlowControl::AppMain^> flowControlMain;
    flowControlMain = gcnew FlowControl::AppMain();
    flowControlMain->Run();
}

and finally inside the .net-Classlib I want to open a Winform-Dialog in a seperate Thread with it's won Messageloop

//opens the WinFowms-Dialog in a new STA-Thread
public static void ShowMyDialog()
{
    Thread thread = new Thread(() =>
    {
        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
        System.Windows.Forms.Application.EnableVisualStyles();

        Dialogs.frmTest frm = new();

        //using ShowDialog or Run does not change behavior
        System.Windows.Forms.DialogResult res = frm.ShowDialog();
        //System.Windows.Forms.Application.Run(frm); // Starte das Fenster mit einer Message-Loop
    });

    thread.SetApartmentState(ApartmentState.STA); // STA needed for WinForms
    thread.Start();
    thread.Join(); // Wait for the Dialog to close
}

It looks like its working: The Dialog shows up and I can use it. But in the output-window of Visual Studio there is a message: Exception thrown at 0x758020F2 (KernelBase.dll) in SPCGUI.exe: 0x8001010D: Ein ausgehender Aufruf kann nicht ausgeführt werden, da die Anwendung einen eingabe-synchronisierten Aufruf weiterleitet. which transltes to "An outgoing call cannot be executed because the application forwards an input-synchronized call."

This looks like something I should not ignore ;-) So whats wrong here and how to open a Dialog correctly?

Upvotes: -2

Views: 49

Answers (0)

Related Questions