c# 线程 WPF 进度百分比(菜鸟)
#region 线程执行 /// <summary> /// 线程运行,将改变的textblock值实时显示在前台 /// </summary> /// <param name="tb"></param> private void Computer(TextBlock tb) { for (int i = 1; i <= 100; i++) { Random rd = new Random(); var num = i; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate () { tb.Text = num + "%"; if (tb.Text == "100%") { tb.Text = "完成"; } }); Thread.Sleep(rd.Next(1, 101)); } } #endregion 线程执行_end
/// <summary> /// 点击开始 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { //将选中的行数据,进度从“0%”--“100%”,然后状态变为完成 //以上Id开始执行 DataGridTemplateColumn column = this.datagrid1.Columns[7] as DataGridTemplateColumn; foreach (var item in this.datagrid1.Items) { var element = column.GetCellContent(item); if (element != null) { TextBlock tb = column.CellTemplate.FindName("Plan", element) as TextBlock; if (bcidL.Where(x => x.Id == Convert.ToInt32(tb.Tag)).Count() > 0) { #region 线程 Thread t = new Thread(() => { Computer(tb); }); t.Start(); #endregion } } } }