Click here to Skip to main content
15,920,576 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
jkirkerx25-Nov-11 15:19
professionaljkirkerx25-Nov-11 15:19 
AnswerRe: WaitForSingleObject, Run do loop, win32 api Pin
Chuck O'Toole25-Nov-11 15:05
Chuck O'Toole25-Nov-11 15:05 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
jkirkerx25-Nov-11 15:20
professionaljkirkerx25-Nov-11 15:20 
AnswerRe: WaitForSingleObject, Run do loop, win32 api Pin
Erudite_Eric26-Nov-11 9:15
Erudite_Eric26-Nov-11 9:15 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
jkirkerx26-Nov-11 12:37
professionaljkirkerx26-Nov-11 12:37 
QuestionRe: WaitForSingleObject, Run do loop, win32 api Pin
Randor 26-Nov-11 13:35
professional Randor 26-Nov-11 13:35 
AnswerRe: WaitForSingleObject, Run do loop, win32 api Pin
jkirkerx26-Nov-11 16:39
professionaljkirkerx26-Nov-11 16:39 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
Randor 27-Nov-11 6:33
professional Randor 27-Nov-11 6:33 
jkirkerx wrote:
The CreateProcess fired off the program, the do loop started and ran my cosmetics 1 time


If you look closely at the code sample I gave you... there is a comment:

C++
//If you are in a GUI thread you may need to pump the messages here


How to pump the message queue C++[^]

I believe the old VB function for this was DoEvents which was probably implemented something like this:

C++
VOID DoEvents()
{
	MSG m;
	while(::PeekMessage(&m, NULL, 0, 0, PM_NOREMOVE))
	{
		if(::GetMessage(&m, NULL, 0, 0))
		{
			::TranslateMessage(&m);
			::DispatchMessage(&m);
		}
		else
		{
			break;
		}
	}
}


If you do not pump the message queue... your dialog will become unresponsive and unable to paint properly. There are typically two ways software engineers tackle this problem:

1.) Put the blocking calls such as WaitForSingleObject in a separate thread.
2.) Wait a small amount of time such as 1 quantum (10 ms on Windows platform) and pump the message queue.

If you copy the code above that pumps the message queue into the do/while loop I gave you...that is waiting 10 milliseconds... your GUI should become responsive. Or you can move everything into a new thread. The choice is yours.

Also... I have noticed that you keep using INFINITE (-1) as the wait time in WaitForSingleObject. This is perfectly correct if you put that function call into a separate thread... however if you are doing this in your dialog window.... WaitForSingleObject will block everything (just as you described).

If you want to continue using a single threaded approach and do all of this from within your dialog window... it is mandatory that you pump the message queue if you want your window to repaint and respond to messages. Try something like this:

C++
STARTUPINFO si;
PROCESS_INFORMATION  pi;
 
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
if(CreateProcess(lpApplicationpath,lpCommandline, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, 0, lpCurrentdirectory, &si, &pi))
{
	while(WAIT_TIMEOUT == WaitForSingleObject(pi.hProcess, 10))
	{
		while(::PeekMessage(&oMSG, NULL, 0, 0, PM_NOREMOVE))
		{
			if(::GetMessage(&oMSG, NULL, 0, 0))
			{
				::TranslateMessage(&oMSG);
				::DispatchMessage(&oMSG);
			}
			else
			{
				break;
			}
		}
	}
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);


Looking at the source code you have presented, I do not see any other problems. In an earlier post you were asking about 'variable clean-up'... your caWin variable is being created on the heap and definitely qualifies as something that should be deleted when your finished with it.

Let me know how it turns out, it is Thanksgiving and I have free time this weekend for getting fat on turkey/pumpkin pie and answering c++ questions. Smile | :)

Best Wishes,
-David Delaune
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
jkirkerx27-Nov-11 9:07
professionaljkirkerx27-Nov-11 9:07 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
Randor 27-Nov-11 9:16
professional Randor 27-Nov-11 9:16 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
jkirkerx27-Nov-11 10:16
professionaljkirkerx27-Nov-11 10:16 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
Randor 27-Nov-11 10:41
professional Randor 27-Nov-11 10:41 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
jkirkerx27-Nov-11 10:50
professionaljkirkerx27-Nov-11 10:50 
GeneralModulus Operator % Pin
Randor 27-Nov-11 11:23
professional Randor 27-Nov-11 11:23 
GeneralRe: WaitForSingleObject, Run do loop, win32 api Pin
jkirkerx27-Nov-11 10:44
professionaljkirkerx27-Nov-11 10:44 
QuestionSetFocus problem Pin
_Flaviu25-Nov-11 3:05
_Flaviu25-Nov-11 3:05 
QuestionRe: SetFocus problem Pin
Chris Meech25-Nov-11 3:23
Chris Meech25-Nov-11 3:23 
AnswerRe: SetFocus problem Pin
_Flaviu25-Nov-11 9:07
_Flaviu25-Nov-11 9:07 
GeneralRe: SetFocus problem Pin
Chris Meech25-Nov-11 9:18
Chris Meech25-Nov-11 9:18 
GeneralRe: SetFocus problem Pin
_Flaviu25-Nov-11 9:46
_Flaviu25-Nov-11 9:46 
GeneralRe: SetFocus problem Pin
Goto_Label_26-Nov-11 11:59
Goto_Label_26-Nov-11 11:59 
AnswerRe: SetFocus problem Pin
Jonathan Davies25-Nov-11 6:44
Jonathan Davies25-Nov-11 6:44 
QuestionCapture mouse over event on trayicon aplication Pin
lucio8124-Nov-11 2:58
lucio8124-Nov-11 2:58 
AnswerRe: Capture mouse over event on trayicon aplication Pin
Software_Developer24-Nov-11 4:56
Software_Developer24-Nov-11 4:56 
AnswerRe: Capture mouse over event on trayicon aplication Pin
Satheesh154624-Nov-11 17:18
Satheesh154624-Nov-11 17:18 

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.