1

I have tried doing things like...

const char *MessageBoxText = "";

DWORD WINAPI CreateMessageBox(LPVOID lpParam){

    MessageBox(NULL, MessageBoxText, "", MB_OK|MB_APPLMODAL);
    return TRUE;

}

MessageBoxText = "Blah, Blah, Blah...";
CreateThread(NULL, 0, &CreateMessageBox, NULL, 0, NULL);

However, this does not seem to work correctly for the task I am trying to perform. What is the best way to create a thread for a message box, without having it glitch up?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
GreenMyth
  • 31
  • 1
  • 4
  • 1
    What is the task you are trying to perform and what do you mean by glitch up? – DrYap Dec 23 '11 at 15:13
  • Well, since the code you posted _doesn't do_ what you're trying to do, how are we supposed to deduce your task from it? I guess you're after a non-modal dialog box. – Lightness Races in Orbit Dec 23 '11 at 15:15
  • I am making a debug program for a python game I am working on, and it has a confirmation message box appear when you press a button, however, if you have more than nine message boxes, you crash, and some people may not exit the message boxes. So by glitching up, I mean it actually crashes. – GreenMyth Dec 23 '11 at 15:16
  • Using a thread makes this problem worse. The message box will have the desktop as its parent since no other windows are created on this thread. And arbitrarily disappear behind another window. – Hans Passant Dec 23 '11 at 16:34

3 Answers3

1

Consider passing message text as thread parameter instead of global variable, to make code thread-safe.

DWORD WINAPI CreateMessageBox(LPVOID lpParam) {
    MessageBoxA(NULL, (char*)lpParam, "", MB_OK);
    return 0;
}
CreateThread(NULL, 0, &CreateMessageBox, "Blah, Blah, Blah...", 0, NULL);

Also, you don't need to specify MB_APPLMODAL as it's default flag (it equals to 0).

If you are targeting modern Windows OS, it's better to have UNICODE defined, because MessageBoxA will convert you strings to UTF-16 and call MessageBoxW

Abyx
  • 12,345
  • 5
  • 44
  • 76
0

Old question, new answer...

If the thread being invoked is being called into existence by a process that immediately exits (say you're running an executable that uses a VC++ DLL which pops a message box) then the thread will never have a chance to execute.

I had created a VC++ executable for testing that ran a loadlibrary on my DLL and the DLL created various threads with debugging message boxes. Since my testing executable only called the DLL and then exited, it didn't work.

  • I was informed by a coworker that I needed to add a sleep to the end of the testing executable that is using the DLL file in order for it to work.

Sure enough, it fixed it. This makes sense too, especially in the context of a DLL. Most applications loop until instructed to exit; my test application did not.

Be mindful of how your application is behaving!

Shrout1
  • 2,497
  • 4
  • 42
  • 65
0

What is the best way to create a thread for a message box, without having it glitch up?

In general, you don't.

Under Windows, all the windows (small 'w', meaning individual GUI elements here) in an application "run" in a single thread. This is generally the "main" thread -- but in particular, it is the thread in which the message pump is running. See my linked post for more details on the message pump.

If what you are truly trying to achieve is a dialog box or some other kind of window that can run concurrently to other windows running, so that it can remain up while other windows are still responsive to user input, then you want a modeless window or dialog box. This is a window that doesn't block other windows from processing updates or accepting user input. See here for a description of how to implement this using MFC, but note that you don't need to use MFC in order to implement a modeless dialog box.

Community
  • 1
  • 1
John Dibling
  • 99,718
  • 31
  • 186
  • 324