Click here to Skip to main content
14,355,062 members

MessageBoxes and worker threads

Rate this:
4.57 (6 votes)
Please Sign up or sign in to vote.
4.57 (6 votes)
25 Oct 2010Apache
MessageBoxes and worker threads

This will hopefully become a more organized article with code samples, but I need to record my results for now. Here’s the practical problem I encountered in the application I am working on: several message boxes initiated by worker threads would pop up simultaneously confusing the user and wrecking havoc.

So, the task is: you want to display a notification (message box) from a worker thread. Sometimes it is fire-and-forget, sometimes you want to know the answer (“Do you want to continue? Yes|No”). I will assume the answer is important, since this case is more complicated.

Here are some facts that I discovered:

Fact #1: Dismissing a message box may enable the parent even if there are other pending message boxes. I am not sure what is the exact algorithm here, but if you have multiple message boxes on the screen, there is at least one of them that would re-enable the parent when dismissed. This will render all other message boxes effectively modeless. The user can continue to work on the parent and maybe even create more message boxes.

Fact #2: Calling MessageBox() from a worker thread effectively converts that thread to a UI thread with the message pump.

Fact #3: If the UI thread is blocked, MessageBox() will also block, even when called on a worker thread. The reason for that is that MessageBox() internally called DialogBox2() function in user32.dll, which calls NtUserCallHwndParamLock(). I am not sure what that function does, but it blocks if the UI thread does not respond.

Fact #4: If you use PostMessage (BeginInvoke) or SendMessage (Invoke) to render the message boxes on the UI thread, the message boxes can still appear in parallel. In fact, they become recursive. If you look at the call stack, it looks like one message box calls another.

This happens because the parent window is not dead while showing a message box: it is just disabled. The message box runs its own message pump, and when another thread sends a message instructing the parent window to show another message box, it will be happily processed, showing the second message box on top of the first.

Bottom line: “serializing” message box calls via PostMessage/SendMessage to the UI thread does not work.

Fact #5. If the UI thread is blocked while we send it “show that message box” messages, the message boxes will appear in sequence, starting with the latest. This puzzled me for a while, but after some research, I found the truth. The message boxes are still being called recursively, only they don't have a chance to show. Showing a window is a complex process that requires handling multiple WM_ messages. When MessageBox() function is called and starts running the inner message loop, there is no window yet. It will appear a little bit later. However, the first message that is processed in the new loop is a request for another message box, which effectively defers creation of the window until later. Only the last message box in the chain has a chance to display itself. Once it is dismissed, previous message box(es) take turn.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

Ivan Krivyakov
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
GeneralBravo Ivan !! Pin
Tacitonitus3-Apr-15 16:14
memberTacitonitus3-Apr-15 16:14 
GeneralMy vote of 1 Pin
.:floyd:.22-Feb-14 12:30
member.:floyd:.22-Feb-14 12:30 
QuestionConvert UI thread back Pin
robiwano21-Feb-14 6:06
memberrobiwano21-Feb-14 6:06 
AnswerRe: Convert UI thread back Pin
Ivan Krivyakov21-Feb-14 11:27
memberIvan Krivyakov21-Feb-14 11:27 
QuestionLeaves a lot of questions open Pin
3soft22-May-13 23:25
member3soft22-May-13 23:25 
AnswerRe: Leaves a lot of questions open Pin
Ivan Krivyakov23-May-13 9:06
memberIvan Krivyakov23-May-13 9:06 
GeneralModal and not modal message boxes Pin
Stefan_Lang2-Nov-10 2:36
memberStefan_Lang2-Nov-10 2:36 
GeneralGood stuff Pin
Manish K. Agarwal27-Oct-10 4:15
memberManish K. Agarwal27-Oct-10 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 25 Oct 2010

Tagged as

Stats

32.7K views
9 bookmarked