Click here to Skip to main content
15,890,186 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Function to pointer problem Pin
_Flaviu9-Aug-12 19:11
_Flaviu9-Aug-12 19:11 
GeneralRe: Function to pointer problem Pin
wangweixu9-Aug-12 20:14
wangweixu9-Aug-12 20:14 
GeneralRe: Function to pointer problem Pin
Joan M9-Aug-12 23:48
professionalJoan M9-Aug-12 23:48 
QuestionGood bootstrapping resources Pin
Brandon-X120009-Aug-12 2:10
Brandon-X120009-Aug-12 2:10 
AnswerRe: Good bootstrapping resources Pin
Randor 9-Aug-12 5:03
professional Randor 9-Aug-12 5:03 
GeneralMessagebox Pin
sarfaraznawaz8-Aug-12 21:24
sarfaraznawaz8-Aug-12 21:24 
QuestionRe: Messagebox Pin
Eugen Podsypalnikov8-Aug-12 21:40
Eugen Podsypalnikov8-Aug-12 21:40 
GeneralRe: Messagebox PinPopular
pasztorpisti8-Aug-12 23:23
pasztorpisti8-Aug-12 23:23 
Accessing gui functions from more than one thread is not a good practice. A gui element (window/control) belongs to a process, and to a thread and a module inside that process. What happens in your case is that you have main gui with its message loop on your main thread, and your messagebox function starts another gui loop on the other thread and god knows what happens with focus management in this case, not to mention that a messagebox is meant to be modal and in your case it isn't!

You should ask the main (gui) thread to manipulate the gui for you by placing a job in its message queue and waiting for that job to finish. In windows you can do that by sending a message for example to your main window with WM_USER+XXX message. The main thread in your main window can handle the message and pop up a messagebox on the main thread. (Note: SendMessage() puts a message to the message queue of the thread of the window and block the execution of the current thread until the other (gui) thread executes the message. PostMessage() just places the message in the queue of the other thread and returns immediately.)

You can write a multithreaded MessageBox() function for yourself that can be called from any threads and does the thing I described above.

For longer gui tasks that must be performed from the middle of your worker thread code you should either use PostMessage() with a WM_USER+XXX message that brings up a modal dialog on the gui thread to do the job (and returns from the WM_USER+XXX message only after this), or if you can not perform the gui task immediately where you handle the WM_USER+XXX message then do this:
Use PostMessage() to send the job with its parameters to your gui thread to its main window, there you can do the gui related stuff and after it you can send the results to the worker thread somehow - for example by declaring a blocking message queue implementation in your worker thread. While the gui is working the worker thread can block on the blocking message queue waiting for the result of the gui task.

Before you start putting in some complicated inter-thread interactions to you program you should consider doing the gui job, and starting the thread only after this with ready-made parameters!

EDIT: You are parenting two popup windows that belong to different threads. This is already stinking, never seen such and wouldn't be surprised if that would be illegal. Use always one thread to create and manipulate gui objects!

EDIT: Popping up a messagebox from a thread at a random point is also a bad design even if it is well implemented technically. Redesing the use cases of your program!

modified 10-Aug-12 2:52am.

GeneralRe: Messagebox Pin
sarfaraznawaz10-Aug-12 1:27
sarfaraznawaz10-Aug-12 1:27 
GeneralRe: Messagebox Pin
pasztorpisti10-Aug-12 2:27
pasztorpisti10-Aug-12 2:27 
GeneralRe: Messagebox Pin
sarfaraznawaz12-Aug-12 19:39
sarfaraznawaz12-Aug-12 19:39 
GeneralRe: Messagebox Pin
pasztorpisti12-Aug-12 19:50
pasztorpisti12-Aug-12 19:50 
GeneralRe: Messagebox Pin
pasztorpisti9-Aug-12 20:55
pasztorpisti9-Aug-12 20:55 
QuestionHow to check if a character is from keyboard in MFC Pin
Andraw1118-Aug-12 10:50
Andraw1118-Aug-12 10:50 
QuestionRe: How to check if a character is from keyboard in MFC Pin
Eugen Podsypalnikov8-Aug-12 11:03
Eugen Podsypalnikov8-Aug-12 11:03 
AnswerRe: How to check if a character is from keyboard in MFC Pin
Software_Developer8-Aug-12 20:05
Software_Developer8-Aug-12 20:05 
GeneralRe: How to check if a character is from keyboard in MFC Pin
Peter_in_27808-Aug-12 20:25
professionalPeter_in_27808-Aug-12 20:25 
GeneralRe: How to check if a character is from keyboard in MFC Pin
Software_Developer8-Aug-12 21:43
Software_Developer8-Aug-12 21:43 
JokeRe: How to check if a character is from keyboard in MFC Pin
Eugen Podsypalnikov8-Aug-12 22:17
Eugen Podsypalnikov8-Aug-12 22:17 
QuestionCB_SELECTSTRING related question Pin
tagopi8-Aug-12 0:24
tagopi8-Aug-12 0:24 
AnswerRe: CB_SELECTSTRING related question Pin
Eugen Podsypalnikov8-Aug-12 0:33
Eugen Podsypalnikov8-Aug-12 0:33 
GeneralRe: CB_SELECTSTRING related question Pin
tagopi8-Aug-12 0:36
tagopi8-Aug-12 0:36 
GeneralRe: CB_SELECTSTRING related question Pin
Eugen Podsypalnikov8-Aug-12 0:42
Eugen Podsypalnikov8-Aug-12 0:42 
GeneralRe: CB_SELECTSTRING related question (solved) Pin
tagopi8-Aug-12 0:53
tagopi8-Aug-12 0:53 
QuestionHow to Dercypt file in C# encrypted in C using Rjindael Algorithm Pin
sangamdumne7-Aug-12 23:52
sangamdumne7-Aug-12 23:52 

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.