原文:WPF 精修篇 长时间线程加取消功能


  1. <Grid>
  2. <Grid.RowDefinitions>
  3. <RowDefinition Height="11*"/>
  4. <RowDefinition Height="29*"/>
  5. </Grid.RowDefinitions>
  6. <StackPanel Orientation="Horizontal" Margin="0" VerticalAlignment="Center">
  7. <Label>开始数据</Label>
  8. <TextBox x:Name="beginText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="100"/>
  9. <Label>结束数据</Label>
  10. <TextBox x:Name="endText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="1000000000" VerticalAlignment="Top" Width="100"/>
  11. <Button x:Name="button" Content="开始" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click"/>
  12. <Button x:Name="Cancel" Content="取消" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Cancel_Click"/>
  13. </StackPanel>
  14. <StackPanel Margin="0" Grid.Row="1">
  15. <TextBlock x:Name="odd" TextWrapping="Wrap" Text="奇数数量:"/>
  16. <TextBlock x:Name="even" TextWrapping="Wrap" Text="偶数数量:"/>
  17. </StackPanel>
  18. </Grid>

  1. private int oddcount =0;
  2. private int evencount =0;
  3. public void Make(int from ,int to)
  4. {
  5. for (int i = from; i < to; i++)
  6. {
  7. if (TokenSource.IsCancellationRequested)
  8. {
  9. evencount = -1;
  10. oddcount = -1;
  11. return;
  12. }
  13. if (i % 2 == 0)
  14. {
  15. evencount++;
  16. }
  17. else
  18. {
  19. oddcount++;
  20. }
  21. }
  22. }
  23. private void Button_Click(object sender, RoutedEventArgs e)
  24. {
  25. int from=0;
  26. int to = 0;
  27. if(int.TryParse(beginText.Text,out from)&&int.TryParse(endText.Text,out to) )
  28. {
  29. button.IsEnabled = false;
  30. ThreadPool.QueueUserWorkItem(_ =>
  31. {
  32. TokenSource = new CancellationTokenSource();
  33. Make(from, to);
  34. Dispatcher.BeginInvoke(new Action(() =>
  35. {
  36. if (oddcount < 0 || evencount < 0)
  37. {
  38. odd.Text = "操作取消";
  39. even.Text = "操作取消";
  40. }
  41. else
  42. {
  43. odd.Text = "奇数数量:" + oddcount;
  44. even.Text = "偶数数量:" + evencount;
  45. }
  46. button.IsEnabled = true;
  47. }));
  48. });
  49. }
  50. }
  51. public CancellationTokenSource TokenSource = null;
  52. private void Cancel_Click(object sender, RoutedEventArgs e)
  53. {
  54. if (TokenSource != null)
  55. {
  56. TokenSource.Cancel();
  57. TokenSource = null;
  58. }
  59. }

最新文章

  1. 关于SIGSEGV错误及处理方法(转)
  2. python enumerate函数用法
  3. iframe与主框架跨域相互访问方法【转】
  4. Project中分清楚挣值项
  5. oralce创建用户
  6. js变量作用域
  7. cas 单点登录配置
  8. tomcat 远程 调试 eclipse
  9. jsp中的动作标签
  10. iOS使用带字体图标的UIButton(支持各种方向)
  11. JAVA WEB之Spring4.x JdbcTemplate
  12. CentOS6.8下MySQL MHA架构搭建笔记
  13. elastic-job详解(三):Job的手动触发功能
  14. spring @Value注解#和$区别
  15. Zookeeper命令行auth,digest
  16. 读书笔记(05) - 事件 - JavaScript高级程序设计
  17. pgm6
  18. form数据请求参数格式
  19. ROM初始化HEX文件
  20. 遇到can not resolve app 依赖包的问题

热门文章

  1. MQTT实战1&#160;- 使用Apache Apollo代理服务器实现mqtt通信
  2. shuffle调优
  3. Lerp在X秒内插值
  4. XML 配置文件,知识点
  5. 整合SpringTask实现定时任务
  6. R-FCN:
  7. LG4819/BZOJ2438 「中山市选2011」杀人游戏 Tarjan缩点+概率
  8. 遍历hashmap 的四种方法
  9. phoenix hbase Can&#39;t get master address from ZooKeeper; znode data == null
  10. elasticsearch入门及安装