I run and stop a process with
Process pr = Process.Start("program.exe") ;
pr.WaitForExit(500) ;
if ( ! pr.HasExited )
pr.Kill();
The problem is that "program.exe" opens a file (for example: test.txt) and with "pr.Kill(); " test.txt remains opened. How can I close this file?
Loading
Narcis ParfenePosted Dec 27, 2009, 5:15 AM
I am working to a project that compile C/C++ programs and then execute them for a given time (for example 0,5 seconds). If that time is exceeded, I want to stop running programs.
Anyway, I succeeded to solve the problem.
Sam HobbsPosted Dec 26, 2009, 4:18 PM
The documentation of the Process.Close Method sure is not clear. Look at the sample code for it; note that it calls CloseMainWindow before Close. It is the CloseMainWindow that is the most important.
You need to ensure that the other program processes the WM_CLOSE message such that the program finishes execution when the message is received. If the other program is a console program then you might need to do something different but I think you can process the WM_CLOSE message properly in a console program too. Is it a console program?
Narcis ParfenePosted Dec 26, 2009, 9:25 AM
Anyway, I don't want to find out WHY the other program is not exiting normally (I intentionally created it in C++ to run for a long time), I just want to stop it after 0.5 seconds and close the files for late deleting.
Sam HobbsPosted Dec 26, 2009, 9:17 AM