Reputation: 3499
I am starting one process from C# code and then i am assign it two different executable to run. I am not sure if this is a good approach. This is the code:
ProcessStartInfo pi = new ProcessStartInfo();
pi.UseShellExecute = true;
pi.Verb = "runas";
pi.FileName = "cmd.exe";
pi.WorkingDirectory = Environment.CurrentDirectory;
Process p = new Process();
p.StartInfo = pi;
p.Start();
ProcessStartInfo p2 = new ProcessStartInfo();
p2.FileName = "notepad.exe";
p2.Verb = "runas";
p.StartInfo = p2;
p.Start();
Console.ReadKey();
Instead of doing this, should i create two instances of Process and assign each of them the corresponding executable to run something like:
Process p1 = Process.Start("cmd.exe");
Process p2 = Process.Start("notepad.exe");
At a first view the first approach seems for me to be better than the second one because i am using only one process instead of two, so less memory and less code but running two different executable in the same process looks a little bit strange for me.
Please let me know your opinion about which approach is the best, and the right one !
Thanks !
Upvotes: 0
Views: 1973
Reputation: 28338
Your second approach is probably better because your first approach isn't saving nearly as much resources as you think.
The Process
class in C# is just a wrapper around the Win32 functions to start/stop/query processes, and is pretty lightweight. The actual running process is completely outside of your application. In both cases, your C# program is starting two additional processes. The only difference is, in your first approach you are discarding all of the useful information you had about the process (it's state, it's PID, it's I/O handles, etc.)
Upvotes: 4