C# process code
-
I have questions to ask about the following C# 2008/2010 code listed below:
Process eProcess = new Process(); eDataContext rData = new eDataContext(); string\[\] PkgIDs = rData.Trans.Select(c => c.Package\_ID ).ToArray(); foreach (string PkgID in PkgIDs) { eProcess.StartInfo.FileName = "app1.exe"; eProcess.StartInfo.Arguments = "10 " + " 5" + PkgID; eProcess.Start(); eProcess.WaitForExit(); eProcess.Close(); }
:wtf: My questions are the following: 1. After the
eProcess.WaitForExit();
line of code is finished waiting for the process to finish executing, is there a way to check for the condition code returned from the run. I basically want to see if the job that executed ran successfully. If so, can you tell me how to setup that code? 2. Do I need the line of code
"eProcess.Close();"
Why or why not? 3. I am basically looping thoough calls executing a process based upon values received in PkgID that is stored in a database table. Is this code good or not? Can you tell me why or why not? If the code is not good, can you tell me a better way to write the code?
-
I have questions to ask about the following C# 2008/2010 code listed below:
Process eProcess = new Process(); eDataContext rData = new eDataContext(); string\[\] PkgIDs = rData.Trans.Select(c => c.Package\_ID ).ToArray(); foreach (string PkgID in PkgIDs) { eProcess.StartInfo.FileName = "app1.exe"; eProcess.StartInfo.Arguments = "10 " + " 5" + PkgID; eProcess.Start(); eProcess.WaitForExit(); eProcess.Close(); }
:wtf: My questions are the following: 1. After the
eProcess.WaitForExit();
line of code is finished waiting for the process to finish executing, is there a way to check for the condition code returned from the run. I basically want to see if the job that executed ran successfully. If so, can you tell me how to setup that code? 2. Do I need the line of code
"eProcess.Close();"
Why or why not? 3. I am basically looping thoough calls executing a process based upon values received in PkgID that is stored in a database table. Is this code good or not? Can you tell me why or why not? If the code is not good, can you tell me a better way to write the code?
Did you happen to read the documentation on the ProcessClass?? There's a little property called "ExitCode" which will contain the exit code of the process once it terminates. You don't need to call Close. It'll be called by the Process class Dispose method automatically, which you SHOULD be calling when you're done with the Process object. You might want to wrap that process code in a
using
block to take care of this for you.A guide to posting questions on CodeProject[^]
Dave Kreskowiak