Shouldn't I Never display a form or a messagebox from a thread?
delphi, delphi-7
Solution
You cannot reliably run VCL forms outside the main GUI thread. When you try to do this, it may appear to work, but at some point in the future such code will fail. Most likely on your most important clients machine, in a particularly critical manner!
The VCL was designed such that all VCL forms are created and operated on from the main GUI thread only. If you need to invoke VCL forms then you have to use tools like `TThread.Synchronize` to make sure that the forms are created on the GUI thread.
Problem
I have always been displaying such things in the main thread and using events to synchronize the end of a window with a thread to let it know when the windows is closed. Today, during development I wanted to move displayed form from thread to main UI, but it successfully was displayed. The only thing I added is that in the main thread I am pushing messages waiting on the event: ``` procedure WaitWithMessageLoop(); var vWaitForEventHandles:array[0..1] of THandle; vWaitForResponse:DWORD; Msg: TMSG; begin vWaitForEventHandles[0] := LServiceMonitor.Handle; while (1=1) do begin vWaitForResponse := MsgWaitForMultipleObjects(1, vWaitForEventHandles, FALSE, INFINITE, QS_ALLINPUT); if (vWaitForResponse = WAIT_OBJECT_0 + 1) then begin while (PeekMessage(msg,0,0,0,PM_REMOVE)) do begin TranslateMessage(msg); DispatchMessage(msg); end; end else if (vWaitForResponse = WAIT_FAILED) then RaiseLastOSError else break; end; end; ``` So my question is. Is a such scenerio acceptable? Or Should I move a form to be displayed in the main thread? Thanks