打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
WPF BackgroundWorker

WPF BackgroundWorker

(2013-01-31 08:55:16)
          private BackgroundWorkerbackgroundWorker;

          public MainWindow()
          {
             InitializeComponent();

             int mainThreadID =Thread.CurrentThread.ManagedThreadId;
             Console.WriteLine("主线程ID:" +mainThreadID);

             backgroundWorker = new BackgroundWorker();
             backgroundWorker.WorkerReportsProgress =true;
             backgroundWorker.WorkerSupportsCancellation =true;
             backgroundWorker.ProgressChanged +=BackgroundWorker_ProgressChanged;
             backgroundWorker.RunWorkerCompleted +=BackgroundWorker_RunWorkerCompleted;
             backgroundWorker.DoWork +=BackgroundWorker_DoWork;
          }

          voidBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
          {
             int backgroundWokrdThreadID =Thread.CurrentThread.ManagedThreadId;
             Console.WriteLine("DoWork BackgroundWorker线程ID:"+ backgroundWokrdThreadID);

             int count = 0;
             //while (true)
             while (count<10)
             {
                 if(backgroundWorker.CancellationPending)
                    return;

                count++;
                backgroundWorker.ReportProgress(count * 10, string.Format("当前进度:{0}%", count * 10));
                Dispatcher.BeginInvoke(new Action(() =>
                 {
                    this.showTB.Text =string.Format("当前进度:{0} %", count * 10);
                }), 
                DispatcherPriority.Normal, null);

             }
          }

          voidBackgroundWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
          {
             int backgroundWokrdThreadID =Thread.CurrentThread.ManagedThreadId;
             Console.WriteLine("RunWorkerCompletedBackgroundWorker线程ID:" + backgroundWokrdThreadID);
          }

          voidBackgroundWorker_ProgressChanged(object sender,ProgressChangedEventArgs e)
          {
             int backgroundWokrdThreadID =Thread.CurrentThread.ManagedThreadId;
             Console.WriteLine("ProgressChangedBackgroundWorker线程ID:" + backgroundWokrdThreadID);

             int progressValue = e.ProgressPercentage;
             
             Console.WriteLine(string.Format("当前进度:{0} %",progressValue));
          }

          private voidStartBackgroundWorker_Click(object sender, RoutedEventArgs e)
          {
             backgroundWorker.RunWorkerAsync();
             
          }
          private voidCancelBackgroundWorker_Click(object sender, RoutedEventArgse)
          {
             backgroundWorker.CancelAsync();
             backgroundWorker.Dispose();
          }


    <GridBackground="Black">
       <ButtonContent="StartBackgroundWorker" HorizontalAlignment="Left"Margin="29,29,0,0" VerticalAlignment="Top" Width="154" Height="33"Click="StartBackgroundWorker_Click"/>
       <TextBlock Name="showTB"HorizontalAlignment="Left" Margin="29,82,0,0" TextWrapping="Wrap"Text="" VerticalAlignment="Top" Height="165" Width="391"
                Background="OrangeRed" Foreground="White"FontSize="24" FontWeight="Bold"/>
       <ButtonContent="CancelBackgroundWorker" HorizontalAlignment="Left"Margin="261,29,0,0" VerticalAlignment="Top" Width="159" Height="33"Click="CancelBackgroundWorker_Click"/>
   </Grid>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
c#多线程Semaphore类旗语锁定
c#实现每隔一段时间执行代码(多线程)-程序开发-红黑联盟
C# 线程池基础上 - 猪悟能'S博客 - 博客园
C#线程本地存储:LocalDataStoreSlot和ThreadLocal<T>
C#多线程:异步事件调用
C#中的异步编程--探索await与async关键字的奥妙之处,原来理解和使用异步编程可以这么简单
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服