zoukankan      html  css  js  c++  java
  • c#多线程,进度条,实时给前台发送数据

    ///做了一个wpf多线程,在实际场景中利用多线程保证程序不会卡死,性能上有所提高

    //启动线程处理
                    Thread thread1 = new Thread(UpdateBtn);
                    thread1.IsBackground = true;//设置为后台线程,当主线程结束后,后台线程自动退出,否则不会退出程序不能结束
                    thread1.Start();

    private void UpdateBtn()
            {
             
              //做时name为datatable循环取值给前台txt追加
                   for (int i = 0; i < name.Rows.Count; i++)
                   {
                       Action action1 = () =>
                   {
                       this.txt.AppendText(name.Rows[i][0].ToString() + "  ");
                       this.txt.AppendText(name.Rows[i][1].ToString() + "  ");
                       this.txt.AppendText(name.Rows[i][2].ToString() + "  ");
                       this.txt.AppendText(name.Rows[i][3].ToString() + "  ");
                       txt.Select(txt.Text.Length, 0);

                       Keyboard.Focus(txt);
                   };

                    //因为主线程在调用所以调用主线程上的委托
                       this.bfb.Dispatcher.Invoke(
                              new Action(
                                  delegate
                                  {
                                      var s = Math.Round((float)i / name.Rows.Count * 100, 2) + "%";
                                      this.bfb.Content = s;

                                  }));      

                       
                     
                       this.txt.Dispatcher.BeginInvoke(action1);
                       SetprogressBar(i);
                       // 如果不设置等待,会导致程序卡死
                       Thread.Sleep(50);
                   }
              
               this.progressBar1.Dispatcher.Invoke(
                               new Action(
                                   delegate
                                   {
                                       bol = true;
                                       System.Windows.MessageBox.Show("执行完毕");
                                       progressBar1.Visibility = Visibility.Hidden;   //隐藏
                                       bfb.Visibility = Visibility.Hidden;   //隐藏

                                   }));          
            }

    附带效果图:
                  

  • 相关阅读:
    条件随机场(crf)及tensorflow代码实例
    Adam作者大革新, 联合Hinton等人推出全新优化方法Lookahead
    33.服务之间的调用之RPC、Restful深入理解
    RPC框架调用过程详解
    Spring 面试问题 TOP 50
    myBatis+Spring+SpringMVC框架面试题整理
    JavaSSM框架精选50道面试题
    maven build的常用生命周期
    玄武短信接口和移动MAS短信接口的API封装
    Java异步执行多个HTTP请求的例子(需要apache http类库)
  • 原文地址:https://www.cnblogs.com/ms1976/p/8618636.html
Copyright © 2011-2022 走看看