Create new process name each iteration

W

wkaibigan

Hi there

I have currently have the code shown below that runs a process and
redirects the output to a listbox. It all works as expected which is
great, however, it is often likely that more than one process will be
required to run, usually up to 4. What I would like is to redirect
the output of each of these processes to individual listboxes.
Currently, a new process is created using

Process app = new Process();

This means that only the output for the last process run gets shown in
the listbox using the current code.

My question is, rather than repeat the same code 4 times using a
different process name for each, is it possible to automatically
change the process name each time the code is called. e.g. when first
run Process app1 = new Process() ; then next run it will be Process
app2 = new Process ; etc.

Hope I made it clear.

Thanks.

Process app = new Process();
string runcommand = textBox1.Text;
app.StartInfo.FileName = version.Location;
app.StartInfo.Arguments = tuflowruntype;
app.StartInfo.RedirectStandardError = true;
app.StartInfo.RedirectStandardOutput = true;
app.StartInfo.CreateNoWindow = true;
app.EnableRaisingEvents = true;
app.ErrorDataReceived += new
DataReceivedEventHandler(app_ErrorDataReceived);
app.OutputDataReceived += new
DataReceivedEventHandler(app_OutputDataReceived);
app.StartInfo.UseShellExecute = false;
app.Start();

textBox3.Text = app.Id.ToString();
listBox1.Items.Add(app.Id.ToString());
switch (comboBox2.SelectedItem.ToString())
{
case "High":
app.PriorityClass =
ProcessPriorityClass.High;
break;
case "Normal":
app.PriorityClass =
ProcessPriorityClass.Normal;
break;
case "Low":
app.PriorityClass =
ProcessPriorityClass.Idle;
break;
}
switch (comboBox3.SelectedItem.ToString())
{
case "All":
app.ProcessorAffinity =
(System.IntPtr)allcpus;
break;
case "0":
app.ProcessorAffinity =
(System.IntPtr)cpu0;
break;
case "1":
app.ProcessorAffinity =
(System.IntPtr)cpu1;
break;
case "2":
app.ProcessorAffinity =
(System.IntPtr)cpu2;
break;
case "3":
app.ProcessorAffinity =
(System.IntPtr)cpu3;
break;
case "4":
app.ProcessorAffinity =
(System.IntPtr)cpu4;
break;
case "5":
app.ProcessorAffinity =
(System.IntPtr)cpu5;
break;
case "6":
app.ProcessorAffinity =
(System.IntPtr)cpu6;
break;
case "7":
app.ProcessorAffinity =
(System.IntPtr)cpu7;
break;
}

app.BeginErrorReadLine();
app.BeginOutputReadLine();
 
B

bradbury9

I think you need to create something like

private void runProcess(string runCommand, Textbox tb, ListBox lb,
DataReceivedEventHandler outputHandler, DataReceivedEventHandler
errorHandler)
{
Process app = new Process();
....
app.ErrorDataReceived += errorHandler;
....
}


And call the method if the correct parameters. Eg.

//First
runProcess(textBox1.Text, textBox3, listBox1, new
DataReceivedEventHandler(app_outputDataReceived), new
DataReceivedEventHandler(app_ErrorDataReceived));
//Second
runProcess(textBox1.Text, textBox3, listBox1, new
DataReceivedEventHandler(app_outputDataReceived), new
DataReceivedEventHandler(app_ErrorDataReceived));
//Third
runProcess(textBox1.Text, textBox3, listBox1, new
DataReceivedEventHandler(app_outputDataReceived), new
DataReceivedEventHandler(app_ErrorDataReceived));
//Fourth
runProcess(textBox1.Text, textBox3, listBox1, new
DataReceivedEventHandler(app_outputDataReceived), new
DataReceivedEventHandler(app_ErrorDataReceived));

if necessary you could create a new class that inherits from
DataReceivedEventhandler that haves more parameters (control to show
info received)
 
W

wkaibigan

I have setup a list of processes as in your example and all works as
far as the processes are concerned. However, I still have the
original issue to deal with. Basically, I run process 1 and redirect
its standard output to Listbox 1. The output from the process updates
in real time within the list box as it should. Then, if I run process
2 and redirect its standard output to Listbox 2, the output from that
process updates in real time in Listbox 2 as expected but, the output
stops updating in Listbox 1. I want to be able to redirect the output
from 1 to 4 processes into 1 to 4 list boxes and have the data in each
of the list boxes update in real time.

Cheers.

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top