So I have been battling this issue for awhile now and tried many different ways to fix it but cannot.
Bascally waht my app does is calls a java file to load an application on a device. While it's loading it's printing it to a richtext box, then I would like to move on to the next file. The problem I am having is that while the first file is being loaded, the 2nd tries to load which cases issues. I have tried the wait for exit but if I do that, then the output data doesn't get written to the rich text box. Any ideas?
private void btnLoad_Click(object sender, EventArgs e)
{
rchsdtOut.Clear();
//count the items in queue.
var count = lstBarToLoad.Items.Count;
if (count <= 0)
{
MessageBox.Show("Nothing added to the load queue");
}
else
{
String toLoad;
for (int i=0; i < count;i++)
{//START OF FOREACH
toLoad = lstBarToLoad.Items[i].Text;
//call load method.
loaddPB(toLoad);
}//end of for.
}//end of else.
}//end of private
I tried putting the wait for exit in many different places, but it doesn't seem to work.
//Method to load files on device.
private void loaddPB(string toLoad)
{
process1 = new System.Diagnostics.Process();
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.RedirectStandardError = true;
process1.StartInfo.CreateNoWindow = true;
process1.StartInfo.FileName = "java.exe ";
process1.StartInfo.Arguments = "-Xmx512M -jar";
process1.StartInfo.Arguments += toLoad;
try
{
process1.Start();
process1.OutputDataReceived += (s, a) => myMethod(a);
process1.BeginOutputReadLine();
process1.ErrorDataReceived += (s, a) => myErrorMethod(a);
process1.BeginErrorReadLine();
process1.WaitForExit();
}
catch
{
Console.WriteLine("error");
}
}
Two methods below write the stdout or error to the richtext field.
//Method to do the logging.
private void myMethod(DataReceivedEventArgs e)
{
if (e.Data != null)
{
Action action = () => rchsdtOut.Text += "\r\n" + e.Data.ToString();
rchsdtOut.BeginInvoke(action, null);
Console.WriteLine(e.Data.ToString());
}
}//end of private
//Method to load the error if any thrown.
private void myErrorMethod(DataReceivedEventArgs e)
{
if (e.Data != null)
{
Action action = () => rchsdtOut.Text += "\r\n" + e.Data.ToString();
rchsdtOut.BeginInvoke(action, null);
Console.WriteLine(e.Data.ToString());
}
}//end of private
Any ideas would be great. Bascally I need the process to exit, so then I can continue thought the forloop to load the next file.