I have a class called GUIMain which registers, creates, and shows a main window for my program. On it is a button which, when the user clicks it, displays an additional window.
If I was writing this in C#, I would have two options:
AdditionalForm myForm = new AdditionalForm();
myForm.ShowDialog(this); // blocking. Returns when myForm is closed;
someOtherFunction();
or
AdditionalForm myForm = new AdditionalForm();
myForm.Show(this); // non-blocking.
someOtherFunction(); // runs while myForm is still visible
or, heck, even this would work:
AdditionalForm myForm = new AdditionalForm();
new Thread(new ThreadStart(delegate()
{
myForm.ShowDialog(this); // blocks in a separate thread
})).Start();
someOtherFunction(); // runs while myForm is still visible
But I’m writing this in C++, not C#.
I have an additional class called PreviewWindow which has the public member functions Register(), Create(), and Show(). The last function contains a message loop which basically makes it a blocking call – analogous to C#’s ShowDialog().
How can I either re-write PreviewWindow::Show() so that it doesn’t have a blocking loop:
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Or alternatively, how can I call it on a separate thread?
I’ve currently got:
previewWindow = new PreviewWindow(hInstance, hWnd);
previewWindow->Register();
previewWindow->Create();
previewWindow->Show(); // blocks :(
previewWindow->DisplayImage(); // never runs.
I have tried CreateThread but it’s not liking that the 3rd argument is a member function. I’ve tried making it a static member function but it doesn’t seem to like that either. It’s saying DWORD (__stdcall *)() isn’t compatible with LPTHREAD_START_ROUTINE.
You should have only one event loop, it will handle all windows except modal ones (which run their own message loop to block the UI). Then your
PreviewWindow::Showis justShowWindow(handle, SW_SHOW);. Multi-threading should generally be saved for when it’s absolutely necessary.