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();
}
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?
Loading
VulpesPosted Sep 7, 2012, 11:20 AM
It's good practice to call Process.Close() as soon as the process is no longer needed because this releases unmanaged resources such as the process handle immediately.
Akkiraju IvaturiPosted Sep 7, 2012, 11:41 AM
By mistake I have added answers to your question in another post after adding the code. Hope the answers by Vulpes satisfies your questions.
This is what MSDN says:
This overload ensures that all processing has been completed, including the handling of asynchronous events for redirected standard output. You should use this overload after a call to the WaitForExit(Int32) overload when standard output has been redirected to asynchronous event handlers.
When an associated process exits (that is, when it is shut down by the operation system through a normal or abnormal termination), the system stores administrative information about the process and returns to the component that had called WaitForExit(). The Process component can then access the information, which includes the ExitTime, by using the Handle to the exited process.
Because the associated process has exited, the Handle property of the component no longer points to an existing process resource. Instead, the handle can be used only to access the operating system's information about the process resource. The system is aware of handles to exited processes that have not been released by Process components, so it keeps the ExitTime and Handle information in memory until the Processcomponent specifically frees the resources. For this reason, any time you call Start for a Process instance, call Close when the associated process has terminated and you no longer need any administrative information about it. Close frees the memory allocated to the exited process.
Sie StePosted Sep 7, 2012, 11:09 AM
eProcess.WaitForExit();
eProcess.Close();
I would think I can replace the two statements above with the following:
eProcess = null;
Do you agree? Why or why not?
VulpesPosted Sep 7, 2012, 10:45 AM
Akkiraju IvaturiPosted Sep 6, 2012, 11:33 PM
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;
class Program
{
static readonly BlockingCollection<string> Collection = new BlockingCollection<string>();
private static int _count = 0;
static void Main()
{
const int maxTasks = 5;//we are going to spawn 5 processes for our example.
var tasks = new List<Task> {
Task.Factory.StartNew(() => {
for(var i = 0; i < 5; i++)
{
Collection.Add(i.ToString(CultureInfo.InvariantCulture));//adding 5 items to the collection object.
}
Console.WriteLine("Spawning multiple processes completed. Now wait and see till all the jobs are completed.");
Collection.CompleteAdding();
}),
};
for (var i = 0; i < maxTasks; i++)
{
tasks.Add(Task.Factory.StartNew(UserTasks(i)));//Add new tasks
}
Task.WaitAll(tasks.ToArray()); // wait for completion
}
///
/// User Tasks method
///
///
///
static Action UserTasks(int id)
{
// return a closure just so the id can get passed
return () =>
{
while (true)
{
string item;
if (Collection.TryTake(out item, -1))
{
using (Process p = new Process())
{
p.StartInfo.FileName = "notepad.exe";
//p.StartInfo.Arguments = item;
p.Start();
p.WaitForExit();
var exitCode = p.ExitCode;
Console.WriteLine(exitCode == 0 ? "{0} exited successfully!!!" : "{0} exited failed!!!", p.Id);
}
}
else if (Collection.IsAddingCompleted)
{
break; // exit loop
}
}
Console.WriteLine("Consumer {0} finished", id);
_count = _count + 1;
if (_count == 4)
{
Console.ReadLine();
}
};
}
}