WPF 精修篇 BackgroundWorker

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 精修篇 BackgroundWorker相关的知识,希望对你有一定的参考价值。

原文: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的主要内容,如果未能解决你的问题,请参考以下文章

WPF 精修篇 用户控件

WPF 精修篇 WPF 使用ActiveX

WPF 精修篇 WPF嵌入Winfrom控件

WPF 精修篇 样式继承

WPF 精修篇 拖拽 DragDrop

WPF 精修篇 属性动画