Click here to Skip to main content
15,887,027 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: how to identify the drive type? Pin
VCProgrammer12-Sep-12 23:16
VCProgrammer12-Sep-12 23:16 
QuestionHelp with threads Pin
AndrewG12314-Sep-12 9:31
AndrewG12314-Sep-12 9:31 
AnswerRe: Help with threads Pin
pasztorpisti4-Sep-12 10:01
pasztorpisti4-Sep-12 10:01 
SuggestionRe: Help with threads Pin
David Crow4-Sep-12 10:09
David Crow4-Sep-12 10:09 
GeneralRe: Help with threads Pin
AndrewG12314-Sep-12 10:17
AndrewG12314-Sep-12 10:17 
SuggestionRe: Help with threads Pin
pasztorpisti4-Sep-12 12:09
pasztorpisti4-Sep-12 12:09 
QuestionRe: Help with threads Pin
AndrewG12314-Sep-12 13:37
AndrewG12314-Sep-12 13:37 
AnswerRe: Help with threads Pin
pasztorpisti4-Sep-12 14:18
pasztorpisti4-Sep-12 14:18 
Its easy. Inside your program every window belongs to a thread - the thread that created the window (note that in a normal program every window is created by a single thread). That thread has a message queue. Every time you do something to the window - click on it with the mouse, request redrawing part of its client area - a message for that window is put in the message queue of the thread that handles that window. The gui thread should remove messages and process them in a loop (this is called the main message loop: GetMessage/DispatchMessage funcionts). In your case MFC does this for you, in pure win32 you do that for yourself. The window messages (WM_LBUTTONDOWN, WM_CLOSE, WM_PAINT, etc...) are also messages that come from that message queue, and when the gui thread encounters such a window message it dispatches that to the windowproc of the right window. OK, how is this related to worker threads? Actually message queues are one of the best things to use as inter-thread communication and fortunately this thread-message queue is thread safe, you can put a message to it from any other thread. For example you can put a window message into the message queue of the gui thread by calling PostMessage() or SendMessage() form your worker thread. Both of these functions put a message to the message queue of the gui thread but PostMessage() returns immediately while SendMessage() blocks the execution of the caller thread and returns only after the gui thread has processed the message. OK, but where to send the message, and what message??? You should define your own window messages by starting from the WM_APP constant. You define for example WM_APP+0 and WM_APP+1 and so on as your thread messages. With PostMessage() and SendMessage() you can send these custom messages to your main window and you can also specify 2 integer parameters: wParam and lParam (lParam is somtimes used as a pointer). For example when you are copying a big file on the worker thread and the percentage counter changes you can use PostMessage() to send a WM_APP+0 message with a wParam=percentage parameter. In your main window you handle the WM_APP+0 message (on your gui thread) and update your progressbar accordingly. Note that we used PostMessage() that put the message to the queue of the gui thread for later processing and PostMessage() returned immediately without unnecessarily waiting for the actual processing of the message on the gui thread (and the progressbur update) so as to prevent slowing down the worker thread. No need to wait for that progressbar update on the gui thread! A codeproject article that demonstrates the use of these thingies with MFC: Synchronization in Multithreaded Applications with MFC[^]

modified 4-Sep-12 20:30pm.

GeneralRe: Help with threads Pin
Joe Woodbury4-Sep-12 17:16
professionalJoe Woodbury4-Sep-12 17:16 
GeneralRe: Help with threads Pin
pasztorpisti4-Sep-12 22:33
pasztorpisti4-Sep-12 22:33 
GeneralRe: Help with threads Pin
Albert Holguin5-Sep-12 12:23
professionalAlbert Holguin5-Sep-12 12:23 
GeneralRe: Help with threads Pin
Richard MacCutchan4-Sep-12 22:06
mveRichard MacCutchan4-Sep-12 22:06 
QuestionRe: Help with threads Pin
David Crow5-Sep-12 3:15
David Crow5-Sep-12 3:15 
AnswerRe: Help with threads Pin
pasztorpisti5-Sep-12 3:31
pasztorpisti5-Sep-12 3:31 
GeneralRe: Help with threads Pin
David Crow5-Sep-12 3:46
David Crow5-Sep-12 3:46 
GeneralRe: Help with threads Pin
pasztorpisti5-Sep-12 3:55
pasztorpisti5-Sep-12 3:55 
GeneralRe: Help with threads Pin
Albert Holguin5-Sep-12 12:26
professionalAlbert Holguin5-Sep-12 12:26 
AnswerRe: Help with threads Pin
Chuck O'Toole5-Sep-12 3:57
Chuck O'Toole5-Sep-12 3:57 
GeneralRe: Help with threads Pin
Albert Holguin5-Sep-12 12:25
professionalAlbert Holguin5-Sep-12 12:25 
AnswerRe: Help with threads Pin
Chuck O'Toole6-Sep-12 15:46
Chuck O'Toole6-Sep-12 15:46 
GeneralRe: Help with threads Pin
Albert Holguin7-Sep-12 3:54
professionalAlbert Holguin7-Sep-12 3:54 
QuestionRe: Help with threads Pin
AndrewG12315-Sep-12 9:55
AndrewG12315-Sep-12 9:55 
AnswerRe: Help with threads Pin
David Crow6-Sep-12 5:41
David Crow6-Sep-12 5:41 
QuestionRe: Help with threads Pin
AndrewG123111-Sep-12 11:33
AndrewG123111-Sep-12 11:33 
QuestionRe: Help with threads Pin
David Crow12-Sep-12 2:45
David Crow12-Sep-12 2:45 

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.