So I am getting this cross thread error and I cannot figure it out. Here is my base code before I attempted to muck around with it.
Bascally what the code is going is calling a batch file which then calls a java file. The outputdata is then redirected to the console in real time. When I just redirect the output just to the C# console, it works fine. But I want the same info to print out into a rich text box within the app. VS 2010 complaines at rchsdtOut.Text = e.Data.ToString(); that Cross-thread operation not valid: Control 'rchsdtOut' accessed from a thread other than the thread it was created on.
I have tried looking this up, and I do admit I am new to threading, so any help on how to easy accomplish this would be appreciated.
//Declare and instantiate a new process component.
System.Diagnostics.Process process1;
process1 = new System.Diagnostics.Process();
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.CreateNoWindow = true;
process1.StartInfo.FileName = "cmd.exe";
process1.StartInfo.Arguments = "BATFile.bat";
process1.OutputDataReceived += (s, a) => myMethod(a);
process1.Start();
process1.BeginOutputReadLine();
process1.WaitForExit();
process1.Close();
private void myMethod(DataReceivedEventArgs e) {
if (e.Data != null)
{
rchsdtOut.Text = e.Data.ToString();
Console.WriteLine(e.Data.ToString());
}
}//end of private