Sign in to follow this  
ehardy

Win32 Process and window focus.

Recommended Posts

Hi, I'm running a process inside of my program (a silent installer actually), and I'm updating a progress bar periodically. Everything seems ok, (well ok for this stage in development anyway) however, I've noticed that if I take the focus off of the app, as it's running the installer, or if i replace the installer with a while loop that breaks after so many seconds, that the program stop responding. To put it more clearly, the while(true) loop that I run to either check the process for termination, or to just tick off a few seconds, seems to stop responding if the focus is taken off of the window.
bool SilentInstall(HWND hPC)
{
	
	STARTUPINFO si;	          // contains window information
	PROCESS_INFORMATION pi;	  // receives information about the new process
	SECURITY_ATTRIBUTES sec;  // security attributes of process

	sec.nLength = sizeof(sec);
	sec.lpSecurityDescriptor = NULL;
	sec.bInheritHandle = TRUE;

	// zero out pointer memory
	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	ZeroMemory( &pi, sizeof(pi) );

	// create process and associate with silent install executable
	if(!CreateProcess("C:\\OWinstall.exe",NULL,&sec,&sec,
             FALSE,0,NULL,NULL,&si, &pi) )
	{
		printf("CreateProcess failed (%d).\n", GetLastError() );
		return false;
	}

	// wait for process to finish
	DWORD exitcode;
	while(TRUE)
	{
		GetExitCodeProcess(pi.hProcess, &exitcode);
		if(exitcode != STILL_ACTIVE )
		{
			break;
		}
		// update progress bar	
		SendMessage(hPC, PBM_STEPIT, 0, 0);
		Sleep(110);	
	}
	
	// exit process and return
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return true;
}

Any information as to why this is happening would be great. I'm new to Win32 programming, and I apologize for any unintentionally horrible bits of code I've written there. :) If I can be more clear or if additional information is needed, please post or email any questions you have. Ernest Hardy

Share this post


Link to post
Share on other sites
Instead of your while loop, why not use WaitForSingleObject() on the HANDLE of the process?

Are you sure that the process doesnt return 259 (the value for STILL_ACTIVE) as an error value or that GetExitCodeProcess() isnt failing?

Share this post


Link to post
Share on other sites
Hi, thanks for the reply.

I've tried using WaitForSingleObject() before, and that was the original way it was written actually, but I'm still having the same problem. I'm wondering if it could be the way in which I'm calling the SilentInstall() method.

The procedure is launched from a menu. Upon selecting it in the menu, a dialog box is displayed, and on clicking OK, then process starts.. like so:



/* ... */
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{

SilentInstall(hPC);
return true;
}
/* ... */



Maybe the while loop, or the WaitForSingleObject call, inside SilentInstall() is preventing the messange handler for the dialog box to function properly when focus is taken off and then reapplied?

Ernest Hardy

Share this post


Link to post
Share on other sites
Since this runs in the same thread as your window procedure your window procedure wont even be processing messages related to focus (or anything else) until SilentInstall() returns.

My original train of thought was that the program becomes unresponsive because SilentInstall() never returns and this would be because the if condition in the while loop is never met. If SilentInstall() does return then perhaps the problem is somewhere else.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this