private void RunStm32Flasher(string arguments, Action <int, string> processFinished) { var errorStream = new MemoryStream(); try { var stm32flashFile = @"stm32flash\stm32flash.exe"; var tpStream = new ThrouputStream(); // STM32Flash process var threadProcessStart = new Thread(() => { var exitCode = ThreadHelperClass.StartProcess( stm32flashFile, arguments, #if !DEBUG timeout: 10000, #endif outputTextWriter: new StreamWriter(tpStream.OutputStream), errorTextWriter: new StreamWriter(errorStream) ).Result; // process Finished _ = BeginInvoke(new Action(() => { errorStream.Seek(0, SeekOrigin.Begin); var errMsg = new StreamReader(errorStream).ReadToEnd(); processFinished(exitCode, errMsg); })); tpStream.Dispose(); }) { Name = "StartProcess" }; threadProcessStart.Start(); var threadReadOutput = new Thread(() => { var tr = new StreamReader(tpStream.InputStream); string line; var processRegex = new Regex(@"(?<name>\w+\s\w+)\s(?<addr>\dx\w+)\s\((?<proc>\d+.\d+)%\)"); while (!tpStream.InputClosed) { line = tr.ReadLine(); if (line == null) { continue; } var mc = processRegex.Match(line); if (mc.Groups.Count != 4) { _ = BeginInvoke(new Action <string>((s) => { rtfStm32Log.Text += $"{s}{Environment.NewLine}"; }), line); } else if (float.TryParse(mc.Groups["proc"].Value, out float proc)) { _ = BeginInvoke(new Action <string, int, string>( (addr, p, l) => { progressBarDownloadProgress.Value = p; progressBarDownloadProgress.CustomText = $"addr:({addr}) [{p}%]"; //rtfStm32Log.Text += $"{l}{Environment.NewLine}"; }) , mc.Groups["addr"].Value, (int)proc, line); } else { _ = BeginInvoke(new Action <string>((s) => { rtfStm32Log.Text += $"{s}{Environment.NewLine}"; }), line); } } //_ = BeginInvoke(new Action<string>((s) => { rtfStm32Log.Text += $"{s}{Environment.NewLine}"; }), "[DONE]"); }) { Name = "ReadOutput" }; threadReadOutput.Start(); } catch (Exception ex) { //MessageBox.Show(ex.Message, $"Fatal error"); processFinished(-1, ex.Message); } }
public InputStreamClass(ThrouputStream parent) { this.parent = parent; this.queue = parent.queue; }