private void ServerProc_ErrorDataReceived(object sender, DataReceivedEventArgs e) { // You have to do this through the Dispatcher because this method is called by a different Thread Dispatcher.Invoke(new Action(() => { ConsoleTextBlock.Text += e.Data + "\r\n"; ConsoleScroll.ScrollToEnd(); })); }
private void console_TextChanged(object sender, TextChangedEventArgs e) { ConsoleScroll.ScrollToEnd(); }
private void WriteToConsole(string text) { ConsoleBar.Text += "\n" + text; ConsoleScroll.ScrollToEnd(); }
/// <summary> /// 窗口被打开时调用的并行计算线程,通过新开线程访问主线程的方式异步更新UI,防止阻塞 /// </summary> private void StartGenetic() { try { //有关于元数据的初始化部分 if (import_xml) { new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add("正从XML文件读取预设元数据……"); })); }).Start(); meta.ReadXML(import_path); new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add("读取完成!"); })); }).Start(); } else { new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add("正由预设信息随机生成元数据……"); })); }).Start(); meta.RandomCreate(map, ashbin, truck, capacity, demand); new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add("生成结束!"); })); }).Start(); if (export_xml) { meta.WriteInXML(export_path); new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add("成功写入到:" + export_path); })); }).Start(); } } //调用算法核心 core.SetValue(population, select_best, cross, transform, new_car); core.Meta = meta; new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add("初始化种群……"); })); }).Start(); core.InitializePopulation(); new Thread(() => { this.Dispatcher.Invoke(new Action(() => { IterationBar.Maximum = iteration; })); }).Start(); double best = core.GlobalShortestDistance; for (int i = 0; i < iteration; i++) { //关闭窗口时发送Cancel指令,每轮循环时检查token if (kill_task.Token.IsCancellationRequested) { break; } core.CaculateFitness(); if (output_style == true) { if (best > core.GlobalShortestDistance) { best = core.GlobalShortestDistance; string message = String.Format("迭代至第{0}轮时,发现了新的最优解:目前的最优解是{1}公里。", i + 1, core.GlobalShortestDistance.ToString("F2")); new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add(message); ConsoleScroll.ScrollToEnd(); })); }).Start(); } core.SelectChildren(); if (operator_choose == true) { core.InverOverTransform(); } else { core.NovelCrossTransform(); } new Thread(() => { this.Dispatcher.Invoke(new Action(() => { IterationBar.Value = i + 1; })); }).Start(); } else { string message = String.Format("当前是第{0}轮,此轮中适应度最高的后代解为{1}公里,目前的最优解是{2}公里。", i + 1, core.PopulationShortestDistance, core.GlobalShortestDistance); core.SelectChildren(); if (operator_choose == true) { core.InverOverTransform(); } else { core.NovelCrossTransform(); } new Thread(() => { this.Dispatcher.Invoke(new Action(() => { IterationBar.Value = i + 1; ConsoleOutputBox.Items.Add(message); ConsoleScroll.ScrollToEnd(); })); }).Start(); } } new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add("迭代完成!"); string message = String.Format("共迭代了{0}轮,最优解为{1}公里", iteration, core.GlobalShortestDistance.ToString("F2")); ConsoleOutputBox.Items.Add(message); ConsoleOutputBox.Items.Add("最优解的路线为:"); List <int> route = core.GlobalShortestRoute; message = string.Format("{0} -> ", route[0]); for (int i = 1; i < route.Count; i++) { if (route[i] == 0) { message += 0; ConsoleOutputBox.Items.Add(message); i++; if (i < route.Count) { message = string.Format("{0} -> ", route[i]); } } else { message += string.Format("{0} -> ", route[i]); } } ConsoleScroll.ScrollToEnd(); })); }).Start(); } catch (Exception anyone) { if (anyone.GetType() is System.Threading.Tasks.TaskCanceledException) { return; } new Thread(() => { this.Dispatcher.Invoke(new Action(() => { ConsoleOutputBox.Items.Add("致命错误:" + anyone.Message); })); }).Start(); } }
private void Console_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) { ConsoleScroll.ScrollToBottom(); }
public async Task ConsoleScrollToBottom() { ConsoleScroll.ScrollToEnd(); ConsoleScroll.ScrollToLeftEnd(); }
private void WriteConsoleOut(string s) { ConsoleOut.AppendText(s); ConsoleScroll.ScrollToVerticalOffset(ConsoleScroll.ExtentHeight); }
private void RichTextBox_OnTextChanged(object sender, TextChangedEventArgs e) { ConsoleScroll.ScrollToEnd(); }