码迷,mamicode.com
首页 > Windows程序 > 详细

WPF 精修篇 BackgroundWorker

时间:2019-12-21 00:23:35      阅读:116      评论:0      收藏:0      [点我收藏+]

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

原文:WPF 精修篇 BackgroundWorker

效果

技术图片

 

  1. <Grid>
  2. <Grid.RowDefinitions>
  3. <RowDefinition Height="22*"/>
  4. <RowDefinition Height="11*"/>
  5. <RowDefinition Height="47*"/>
  6. </Grid.RowDefinitions>
  7. <StackPanel Orientation="Horizontal" Margin="0,28" VerticalAlignment="Center">
  8. <Label>开始数据</Label>
  9. <TextBox x:Name="beginText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="100"/>
  10. <Label>结束数据</Label>
  11. <TextBox x:Name="endText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="1000000000" VerticalAlignment="Top" Width="100"/>
  12. <Button x:Name="button" Content="开始" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click"/>
  13. <Button x:Name="Cancel" Content="取消" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Cancel_Click"/>
  14. </StackPanel>
  15. <StackPanel Margin="0" Grid.Row="1" Grid.RowSpan="2">
  16. <TextBlock x:Name="odd" TextWrapping="Wrap" Text="奇数数量:"/>
  17. <TextBlock x:Name="even" TextWrapping="Wrap" Text="偶数数量:"/>
  18. <ProgressBar x:Name="Prebar" Height="20"/>
  19. </StackPanel>
  20. </Grid>
  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 < to; 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 Tuple<int, 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 = ( Tuple<int, 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. }

 

WPF 精修篇 BackgroundWorker

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!