I need to display a C# form with a message 'Please wait' and then perform additional tasks at the background. When these additional tasks complete, I then have to hide/close that form. I am doing this in c++ console application. I am calling the functions to display and close the form, which are defined in C# and exposed using UnmanagedExports. Problem is that the form is not displaying properly (all controls are not loaded) and when the cursor is brought on form it shows the loading state. How can I display the form properly, perform the tasks and then close the form?
C++ code:
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# code:
[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();
}
I managed to solve the issue by adding Application.DoEvents(); after form.show(); Now it is working fine and the form is showing properly. Now the final showForm c# code becomes:
NativeWindow nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(owner);
Form_Wait form = new Form_Wait();
form.label_message.Text = message;
form.Show(nativeWindow);
Application.DoEvents();
Regards