WPF 精修篇 BackgroundWorker

2021-01-23 06:13

阅读:560

标签:imp   title   label   cal   keyword   ndt   登录   ble   run   

原文:WPF 精修篇 BackgroundWorker

效果

技术图片

 

  1. "22*"/>
  2. "11*"/>
  3. "47*"/>
  4. "Horizontal" Margin="0,28" VerticalAlignment="Center">
  5. "beginText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="100"/>
  6. "endText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="1000000000" VerticalAlignment="Top" Width="100"/>
  7. "0" Grid.Row="1" Grid.RowSpan="2">
  8. "odd" TextWrapping="Wrap" Text="奇数数量:"/>
  9. "even" TextWrapping="Wrap" Text="偶数数量:"/>
  10. "Prebar" Height="20"/>
  1. private int oddcount =0;
  2. private int evencount =0;
  3. public void Make(int from ,int to)
  4. {
  5. oddcount = 0;
  6. evencount = 0;
  7. int num = (to - from) / 100;
  8. for (int i = from; i
  9. {
  10. if (worker.CancellationPending == true)
  11. {
  12. return;
  13. }
  14. if (i % 2 == 0)
  15. {
  16. evencount++;
  17. }
  18. else
  19. {
  20. oddcount++;
  21. }
  22. if (i % num == 0 && (worker != null) && (worker.WorkerReportsProgress = true))
  23. {
  24. worker.ReportProgress(i / num);
  25. }
  26. }
  27. }
  28. BackgroundWorker worker = null;
  29. private void Button_Click(object sender, RoutedEventArgs e)
  30. {
  31. int from=0;
  32. int to = 0;
  33. if(int.TryParse(beginText.Text,out from)&&int.TryParse(endText.Text,out to) )
  34. {
  35. button.IsEnabled = false;
  36. odd.Text = "奇数数量:0" ;
  37. even.Text = "偶数数量:0" ;
  38. worker = new BackgroundWorker();
  39. //异步取消 需要增加这个 不然取消失效
  40. worker.WorkerSupportsCancellation = true;
  41. //支持报告进度
  42. worker.WorkerReportsProgress = true;
  43. //注册滚动条事件
  44. worker.ProgressChanged+=worker_ProgressChanged;
  45. //注册任务
  46. worker.DoWork+=worker_DoWork;
  47. //任务完毕触发
  48. worker.RunWorkerCompleted += worker_RunWorkerCompleted;
  49. //给任务传参
  50. worker.RunWorkerAsync(new Tupleint, int>(from, to));
  51. }
  52. }
  53. private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
  54. {
  55. Prebar.Value = e.ProgressPercentage;
  56. }
  57. void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  58. {
  59. // throw new NotImplementedException();
  60. if (sender is BackgroundWorker)
  61. {
  62. if (e.Cancelled == true)
  63. {
  64. odd.Text = "任务已取消";
  65. even.Text = "";
  66. }
  67. else
  68. {
  69. odd.Text = "奇数数量:" + oddcount;
  70. even.Text = "偶数数量:" + evencount;
  71. }
  72. BackgroundWorker k = (BackgroundWorker)sender;
  73. k.DoWork -= worker_DoWork;
  74. k.RunWorkerCompleted -= worker_RunWorkerCompleted;
  75. k = null;
  76. button.IsEnabled = true;
  77. }
  78. }
  79. private void worker_DoWork(object sender, DoWorkEventArgs e)
  80. {
  81. var args = ( Tupleint, int>)e.Argument;
  82. Make(args.Item1, args.Item2);
  83. if (worker.CancellationPending == true)
  84. {
  85. e.Cancel = true;
  86. }
  87. }
  88. private void Cancel_Click(object sender, RoutedEventArgs e)
  89. {
  90. worker.CancelAsync();
  91. }
  92.  

    WPF 精修篇 BackgroundWorker

    标签:imp   title   label   cal   keyword   ndt   登录   ble   run   

    原文地址:https://www.cnblogs.com/lonelyxmas/p/12075538.html


评论


亲,登录后才可以留言!

推荐文章

最新文章

置顶文章