I wrote a small program:
Thread1 thread:
public void ExecuteCommandSync(string argument)
{
try
{
var procStartInfo = new System.Diagnostics.ProcessStartInfo("bbb.bat", argument);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.ErrorDialog = false;
var proc = new System.Diagnostics.Process();
proc.OutputDataReceived += (s, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
LogBox.BeginInvoke(new EventHandler(delegate { LogBox.AppendText(e.Data.ToString() + Environment.NewLine); }));
LogBox.BeginInvoke(new EventHandler(delegate { LogBox.ScrollToCaret(); }));
}
};
proc.StartInfo = procStartInfo;
proc.Start();
proc.BeginOutputReadLine();
proc.WaitForExit();
}
catch (Exception objException)
{
Console.WriteLine("Error: " + objException.Message);
}
}
UI Thread:
var command = new Thread(() => ExecuteCommandSync(argument));
command.Start();
command.Join();
while (!command.IsAlive)
{
if (LogBox.Text.Contains("error code 0"))
{ AddMessage("\r\n" + "Operation successful.\r\n", Color.DarkGreen, FontStyle.Bold); }
else
{ AddMessage("\r\n" + "Operation unsuccessful.\r\n", Color.DarkRed, FontStyle.Bold); }
LogBox.ScrollToCaret();
path = null;
button1.Enabled = false;
break;
}
The problem is that
Thread1 thread should be finished before the UI thread shows "operation successful/unsuccessful message"
With the code above, UI thread shows that message before thr results from Thread1 :(((((
How to make it in correct order?
Thx for help in advance!