Click here to Skip to main content
15,903,201 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Name of this DesignPattern Pin
jschell12-Aug-13 8:08
jschell12-Aug-13 8:08 
QuestionQT C1128: number of sections exceeded object file format limit : compile with /bigobj Pin
onur dalkilic11-Aug-13 22:13
onur dalkilic11-Aug-13 22:13 
AnswerRe: QT C1128: number of sections exceeded object file format limit : compile with /bigobj Pin
«_Superman_»11-Aug-13 22:31
professional«_Superman_»11-Aug-13 22:31 
AnswerRe: QT C1128: number of sections exceeded object file format limit : compile with /bigobj Pin
Stephen Hewitt12-Aug-13 16:15
Stephen Hewitt12-Aug-13 16:15 
Questionusing scardAPI how to get DeviceInstanceId Pin
xiliang_pan11-Aug-13 3:40
xiliang_pan11-Aug-13 3:40 
QuestionHow to use an event object for synchronization.? Pin
mbatra319-Aug-13 23:42
mbatra319-Aug-13 23:42 
AnswerRe: How to use an event object for synchronization.? Pin
Richard Andrew x6410-Aug-13 5:39
professionalRichard Andrew x6410-Aug-13 5:39 
AnswerRe: How to use an event object for synchronization.? Pin
pasztorpisti10-Aug-13 10:13
pasztorpisti10-Aug-13 10:13 
There are several possible solutions to this problem on windows but maybe the simplest that came to my mind when I read your post is the following:

This task has nothing to do with command lines. Its an unimportant detail. You have your program running in its own process and you are launching another process with CreateProcess(). As a result you have a process handle to the other process. You can use this handle to wait for the termination of the other process for infinitely long time, for a specified amount of time (timeout), or you can poll this handle if the specified timeout value is zero using the WaitForSingleObject() or WaitForMultipleObjects() functions with the process handle. I would go by periodically (1-10 times per second) polling the handle from the gui thread, you can do that easily for example by setting up a WM_TIMER message for this purpose. Depending on your goals you can stop waiting for the running program anytime by stopping the WM_TIMER message and closing the handle and the dialog.

Since you have to be able to close the dialog yourself programmatically you can not use the MessageBox api that closes itself as a result of user input. You have to use your own modal or modeless dialog for this purpose. If you don't want the user to be able to interact with the other windows of your program while waiting then you need a modal dialog. If you don't want the user to be able to close your dialog by pressing X or by pressing alt+f4 then you have to handle the WM_CLOSE message by simply doing nothing (by not calling DestroyWindow(), EndDialog() or something similar).

So:
- launch the program with CreateProcess() and store the process handle for later use. Don't forget to close the handle with CloseHandle() when you no longer need it!
- At the same time you launch the process show your dialog and setup the WM_TIMER.
- In your the WM_TIMER message handler to you call WaitForSingleObject(process_handle, 0) and if the return value is WAIT_OBJECT_0 then the child process has finished so its okay to close the dialog.

At any time in your program you can stop waiting for example if you put some extra buttons to your dialog like "Stop waiting and let the child process run", or "Stop waiting and terminate child process"

Often there is no need for multithreading in case of multiprocessing. I think even the progress update is possible to solve from your WM_TIMER message handler, the question is the method we use to transfer this small piece of data (the progress) from the child process to the parent. The first two solutions that came to my mind:
- PostMessage / SendMessage mentioned by R. Andrew, I recommend PostMessage whenever possible to avoid unnecessary blocking of the child process. For larger messages use WM_COPYDATA.
You have to pass the HWND value to the child process in order to be able to do this for example on commandline or as an env var.
- dumbass solution: writing the progress into a specified file (either as text or binary integer) and from your wm_timer you always open the file, read out the stuff and show it.
- nonblocking pipe
- redirection of the output (stdout and/or stderr) of the child process, can be async or multithreaded

The correct implementation of communication between the two processes is probably the most complicated thing. Is the code of the child process yours, are you allowed to modify it or you have to parse the output of an existing prog to gather the progress?
GeneralRe: How to use an event object for synchronization.? Pin
mbatra3111-Aug-13 21:18
mbatra3111-Aug-13 21:18 
GeneralRe: How to use an event object for synchronization.? Pin
pasztorpisti12-Aug-13 5:42
pasztorpisti12-Aug-13 5:42 
GeneralRe: How to use an event object for synchronization.? Pin
Erudite_Eric16-Aug-13 1:30
Erudite_Eric16-Aug-13 1:30 
GeneralRe: How to use an event object for synchronization.? Pin
pasztorpisti16-Aug-13 1:53
pasztorpisti16-Aug-13 1:53 
AnswerRe: How to use an event object for synchronization.? Pin
Krishnakumartg12-Aug-13 6:15
Krishnakumartg12-Aug-13 6:15 
AnswerRe: How to use an event object for synchronization.? Pin
Erudite_Eric16-Aug-13 1:32
Erudite_Eric16-Aug-13 1:32 
GeneralRe: How to use an event object for synchronization.? Pin
pasztorpisti16-Aug-13 1:52
pasztorpisti16-Aug-13 1:52 
GeneralRe: How to use an event object for synchronization.? Pin
Erudite_Eric16-Aug-13 23:02
Erudite_Eric16-Aug-13 23:02 
GeneralRe: How to use an event object for synchronization.? Pin
pasztorpisti17-Aug-13 1:31
pasztorpisti17-Aug-13 1:31 
GeneralRe: How to use an event object for synchronization.? Pin
mbatra3126-Dec-13 19:35
mbatra3126-Dec-13 19:35 
GeneralRe: How to use an event object for synchronization.? Pin
mbatra3126-Dec-13 19:34
mbatra3126-Dec-13 19:34 
QuestionCButton and CListCtrl two controls how self drawing Pin
ztwisdom9-Aug-13 16:17
ztwisdom9-Aug-13 16:17 
SuggestionRe: CButton and CListCtrl two controls how self drawing Pin
Richard MacCutchan9-Aug-13 23:28
mveRichard MacCutchan9-Aug-13 23:28 
AnswerRe: CButton and CListCtrl two controls how self drawing Pin
Krishnakumartg12-Aug-13 6:17
Krishnakumartg12-Aug-13 6:17 
QuestionChanging Treeview control apperance Pin
Member 78617636-Aug-13 22:58
Member 78617636-Aug-13 22:58 
AnswerRe: Changing Treeview control apperance Pin
_AnsHUMAN_ 7-Aug-13 2:10
_AnsHUMAN_ 7-Aug-13 2:10 
GeneralRe: Changing Treeview control apperance Pin
Member 78617637-Aug-13 3:38
Member 78617637-Aug-13 3:38 

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.