0

我需要显示带有消息“请稍候”的 C# 表单,然后在后台执行其他任务。当这些附加任务完成后,我必须隐藏/关闭该表单。我在 C++ 控制台应用程序中执行此操作。我正在调用函数来显示和关闭表单,这些函数在 C# 中定义并使用 UnmanagedExports 公开。问题是表单没有正确显示(所有控件都没有加载),当光标出现在表单上时,它会显示加载状态。如何正确显示表单、执行任务然后关闭表单?

C++ 代码:

using CSharpFormShow = void(__stdcall *)(HWND hwnd, wchar_t* message);
using CSharpFormClose = void(__stdcall *)();
int _tmain(int argc, _TCHAR* argv[])
{
    HMODULE mod = LoadLibraryA("CSharp.dll");
    CSharpFormShow formShow = reinterpret_cast<CSharpFormShow>(GetProcAddress(mod, "formshow"));
    CSharpFormClose formClose = reinterpret_cast<CSharpFormClose>(GetProcAddress(mod, "formclose"));
    formShow(hwnd,L"This is a message");
    //perform some tasks
    formClose();
    getchar();
    return 0;
}

C#代码:

[DllExport(ExportName = "formshow", CallingConvention = CallingConvention.StdCall)]
public static void showForm(IntPtr owner, [MarshalAs(UnmanagedType.LPWStr)]string message)
{
    NativeWindow nativeWindow = new NativeWindow();
    nativeWindow.AssignHandle(owner);
    Form_Wait form = new Form_Wait();
    form.label_message.Text = message;
    form.Show(nativeWindow);
}
[DllExport(ExportName = "formclose", CallingConvention = CallingConvention.StdCall)]
public static void closeForm()
{
    form.Dispose();
}
4

1 回答 1

0

我设法通过添加 Application.DoEvents(); 解决了这个问题。在 form.show() 之后;现在它工作正常并且表格显示正确。现在最终的 showForm c# 代码变为:

NativeWindow nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(owner);
Form_Wait form = new Form_Wait();
form.label_message.Text = message;
form.Show(nativeWindow);
Application.DoEvents();

问候

于 2018-09-04T19:39:30.977 回答