Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, March 28, 2006 10:12 AM
Hi,
I want to run multiple processes (for example 10 processes who get started after each other) after a button is pressed from a Windows form. Each process executes the same executable.
For example:
Process 1: ipconfig /all
Process 2: ipconfig /release
Process 3: ipconfig /renew
Etc.
I know I should use the Process class, but how can I do this?
I don't think (and I don't know how) I can use a loop, because the commands are different.
Anyone who can help me out?
Thanks in advance!
All replies (7)
Tuesday, March 28, 2006 12:27 PM ✅Answered | 1 vote
Code:
public Process[] StartProcesses( ProcessStartInfo[] infos, bool waitForExit ) { ArrayList processesBuffer = new ArrayList(); foreach( ProcessStartInfo info in infos ) { Process process = Process.Start( info ); if( waitForExit ) { process.WaitForExit(); } processesBuffer.Add( process ); } return (Process[])processesBuffer.ToArray( typeof(Process) ); } |
Usage:
ProcessStartInfo[] infos = new ProcessStartInfo[] { new ProcessStartInfo( "notepad", "c:\textfile.txt" ), new ProcessStartInfo( @"c:\myapp.exe", "-a -i" ), new ProcessStartInfo( @"f:\backup.exe", "-t TAPE1" ), }; Process[] startedProcesses = StartProcesses( infos, true ); |
Wednesday, March 29, 2006 9:14 AM ✅Answered
You set the start info after starting, then it hasn't any effect. Here is the modified code:
public Process[] StartProcesses(ProcessStartInfo[] infos) { ArrayList processesBuffer = new ArrayList(); foreach(ProcessStartInfo info in infos) { // Make sure the Hidden flag is set. info.WindowStyle = ProcessWindowStyle.Hidden; // Start process and add to return buffer. Process process = Process.Start(info); processesBuffer.Add(process); } return(Process[])processesBuffer.ToArray(typeof(Process)); } |
Tuesday, March 28, 2006 12:07 PM
Hi
I think this might help.
private void button1_Click(object sender, EventArgs e)
{
Process proc_1 = new Process();
Process proc_2 = new Process();
Process proc_3 = new Process();
proc_1.StartInfo.FileName = ("Notepad");
proc_2.StartInfo.FileName = ("Excel");
proc_3.StartInfo.FileName = ("WINWORD");
proc_1.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc_2.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc_3.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
ArrayList processes = new ArrayList();
processes.Add(proc_1);
processes.Add(proc_2);
processes.Add(proc_3);
foreach (Process proc in processes)
{
proc_1.Start();
//proc_1.WaitForExit(); //Wait for notepad to close
proc_2.Start();
//proc_2.WaitForExit();
proc_3.Start();
//proc_3.WaitForExit();
}
}
Wednesday, March 29, 2006 8:27 AM
Thanks PJ, it's working!
One more thing, how can I remove WaitForExit, and include ProcessWindowStyle.Hidden into the code above?
I tried the following, but the DOS window still pops up:
public Process[] StartProcesses(ProcessStartInfo[] infos)
{
ArrayList processesBuffer = new ArrayList();
foreach(ProcessStartInfo info in infos)
{
Process process = Process.Start(info);
info.WindowStyle = ProcessWindowStyle.Hidden;
processesBuffer.Add(process);
}
return(Process[])processesBuffer.ToArray(typeof(Process));
}
The executable I want to run resides in the system32 folder.
Do I have to use Request.MapPath, or does it automatically look in the system32 folder first?
Thanks again!
Wednesday, March 29, 2006 9:19 AM
Arg, doh.
You're the best, thanks again!
Thursday, May 20, 2010 9:44 PM
I am very sorry to revive this very old thread but I have a question regarding your solution.
I followed your code and on mine, I have to specify "Static" on the method because the the compiler was barking about it, as in:
public static Process[] StartProcesses(ProcessStartInfo[] infos)
{
}
Does this make any sense?
Let me know.
Thanks.
Friday, May 21, 2010 10:10 AM
I think you're calling the StartProcesses method from another static method (for example, from the Main method).
Since StartProcesses never uses this, it should propably be static anyway.