Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process and thread issues

Status
Not open for further replies.

bkowlagi

Programmer
Apr 8, 2003
28
US
I have coded a program in Win32 API which essentially a information gathering tool. there is this dialog in which the user is supposed to enter info. A separate thread is spawned which creates a process running another executable in the background. Now the question is if the user clicks cancel in the middle the program is supposed to exit and delete the directory which contains the background executable. I have tried both the terminateprocess and the exitprocess to stop the background thread and then call the function i have written to delete the folder. currently all the files in folder delete except the background exe, hence the folder is deleted as well.
 
You might want to force waiting a few seconds, to allow the IO mechanism to realize the file is no longer in use.
 
I tried that by using sleep. but I think when I give sleep to the main thread/process. the background process is halted as well which basically does nothing and it is the same. So how do i get the main thread/process to Sleep and yet allow the child thread/process to finish up cleaning upon getting the terminate event. Thanks anyway.
 
Some hints:

1. do NOT call ExitProcess, this will exit only the current process and not the background process.
2. Letting the main process Sleep does NOT let the child process sleep as well. In fact, the child is independent from it's parent and does continue even when the parent terminates.
3. Calling TerminateProcess must only be used in an emergency. Dll's attached to the terminated process are not notified the process terminates and may not free resources. Perhaps this the cause of your trouble? It is better trying to let the child process terminate normally by sending it a WM_CLOSE message or something like that.
4. Wait until the child process is really terminated, and do not forget to close it's handle. Something like:
TerminateProcess ( hProcess, ExitCode );
// Or SendMessage ( ..., WM_CLOSE, ... );

// Wait max. 10 seconds for the termination
if ( WaitForSingleObject ( hProcess, 10000 ) == WAIT_TIMEOUT )
{ // Terminating the process failed, handle this here
....
}
// Process terminated successfully
CloseHandle ( hProcess );
// Now delete the folder


Marcel
 
It worked. though even after tweaking the wait times all the files are now getting deleted, only the folder remains. So i'm left with a empty folder to delete manually. This is ok. Thanks all for the help.
 
After deleting all the files, put something like

char DirName[] = "C:\\whatever\\it\\is" );
RemoveDirectory ( DirName );



Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top