原文: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. }

WPF 精修篇 长时间线程加取消功能的更多相关文章

  1. WPF 精修篇 page

    原文:WPF 精修篇 page 前言 前段时间看UML 大象 这本书 虽然马上看到了精华片 最后还是暂时暂停 因为这本书 很好 但是暂时对现在的我来说 有点超前 很多东西理解起来还是很难 但是 这本书 ...

  2. WPF 精修篇 WPF嵌入Winfrom控件

    原文:WPF 精修篇 WPF嵌入Winfrom控件 先增加DLL 支持 使用  WindowsFormsHost 来加载Forms的控件 引用命名空间 xmlns:forms="clr-na ...

  3. WPF 精修篇 BackgroundWorker

    原文:WPF 精修篇 BackgroundWorker 效果 <Grid> <Grid.RowDefinitions> <RowDefinition Height=&qu ...

  4. WPF 精修篇 非UI进程后台更新UI进程

    原文:WPF 精修篇 非UI进程后台更新UI进程 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11* ...

  5. WPF 精修篇 多属性触发器

    原文:WPF 精修篇 多属性触发器 多属性触发器就是多个属性都满足在触发 在属性触发器上加了一些逻辑判断 举栗子 这个栗子里  textBox 要满足俩个条件 才能触发背景变色 1)textbox的 ...

  6. WPF 精修篇 数据触发器

    原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style Target ...

  7. WPF 精修篇 属性触发器

    原文:WPF 精修篇 属性触发器 属性触发器是通过  某个条件触发改变属性 通过无代码实现功能 <Style TargetType="{x:Type Label}"> ...

  8. WPF 精修篇 样式继承

    原文:WPF 精修篇 样式继承 这个 是新知识 样式可以继承 <Style x:Key="TextBlockStyleBase" TargetType="{x:Ty ...

  9. WPF 精修篇 自定义控件

    原文:WPF 精修篇 自定义控件 自定义控件 因为没有办法对界面可视化编辑 所以用来很少 现在实现的是 自定义控件的 自定义属性 和自定义方法 用VS 创建自定义控件后 会自动创建 Themes 文件 ...

随机推荐

  1. rt-thread can

    rt-thread stm32f10x-HAL can的驱动和应用.源码暂时还不支持,自己通过F4修改了一版 源码地址:https://gitee.com/gitee.thomas/rt_can rt ...

  2. dos转unix

    方式一 # yum install dos2unix.x86_64 # dos2unix file 方式二 查看样式: :set ff? //dos/unix 设置: :set fileformat= ...

  3. 怎样解决非管理员账户添加Notepad++右键菜单的批处理的问题?

    bat脚本如下: @echo off color 1e title 将Notepad++增加到右键菜单(或者去关联) goto :menu :menu cls echo. echo. 1 将Notep ...

  4. Transformer —— attention is all you need

    https://www.cnblogs.com/rucwxb/p/10277217.html Transformer -- attention is all you need Transformer模 ...

  5. vue 开发系列(十) VUE 作用域插槽

    使用场景 官方解释,有时让插槽内容能够访问子组件中才有的数据是很有用的.比如我们在使用ant-design-vue 的表格控件时. <a-table-column title="注释& ...

  6. linux设备驱动程序-设备树(2)-device_node转换成platform_device

    设备树处理之--device_node转换成platform_device 以下讨论基于linux4.14,arm平台 platform device 设备树的产生就是为了替代driver中过多的pl ...

  7. 第24课经典问题(中)-----关于const对象的疑问

    关于const对象的疑问const关键字能否修饰类的对象?如果可以,有什么特性?const关键字能够修饰对象const修饰的对象为只读对象只读对象的成员变量不允许被改变.(对象是只读的,成员变量不允许 ...

  8. 在 Oracle 中使用正则表达式

    Oracle使用正则表达式离不开这4个函数: 1.regexp_like 2.regexp_substr 3.regexp_instr 4.regexp_replace 看函数名称大概就能猜到有什么用 ...

  9. 常用开窗函数总结(hive、sparkSQL可执行)

    一:根据某个字段排序 测试数据: SQL> select * from sscore; NAME       SCORE ---------- ----- aa            99 bb ...

  10. RST复位报文

    复位报文段: 一些特殊情况,TCP一端向另一端发送复位报文,以通知对方关闭链接或者重新建立链接. 产生复位报文的三种情况: 1. 当客户端访问一个不存在的端口时,目标主机会给客户端发送一个复位报文段. ...