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

-
<Grid>
-
<Grid.RowDefinitions>
-
<RowDefinition Height="11*"/>
-
<RowDefinition Height="29*"/>
-
</Grid.RowDefinitions>
-
<StackPanel Orientation="Horizontal" Margin="0" VerticalAlignment="Center">
-
<Label>开始数据</Label>
-
<TextBox x:Name="beginText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="100"/>
-
<Label>结束数据</Label>
-
<TextBox x:Name="endText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="1000000000" VerticalAlignment="Top" Width="100"/>
-
<Button x:Name="button" Content="开始" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click"/>
-
<Button x:Name="Cancel" Content="取消" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Cancel_Click"/>
-
</StackPanel>
-
<StackPanel Margin="0" Grid.Row="1">
-
<TextBlock x:Name="odd" TextWrapping="Wrap" Text="奇数数量:"/>
-
<TextBlock x:Name="even" TextWrapping="Wrap" Text="偶数数量:"/>
-
</StackPanel>
-
</Grid>
-
private int oddcount =0;
-
private int evencount =0;
-
public void Make(int from ,int to)
-
{
-
for (int i = from; i < to; i++)
-
{
-
if (TokenSource.IsCancellationRequested)
-
{
-
evencount = -1;
-
oddcount = -1;
-
return;
-
}
-
if (i % 2 == 0)
-
{
-
evencount++;
-
}
-
else
-
{
-
oddcount++;
-
}
-
}
-
}
-
private void Button_Click(object sender, RoutedEventArgs e)
-
{
-
int from=0;
-
int to = 0;
-
if(int.TryParse(beginText.Text,out from)&&int.TryParse(endText.Text,out to) )
-
{
-
button.IsEnabled = false;
-
ThreadPool.QueueUserWorkItem(_ =>
-
{
-
TokenSource = new CancellationTokenSource();
-
Make(from, to);
-
-
Dispatcher.BeginInvoke(new Action(() =>
-
{
-
if (oddcount < 0 || evencount < 0)
-
{
-
odd.Text = "操作取消";
-
even.Text = "操作取消";
-
}
-
else
-
{
-
odd.Text = "奇数数量:" + oddcount;
-
even.Text = "偶数数量:" + evencount;
-
}
-
button.IsEnabled = true;
-
}));
-
-
});
-
-
}
-
-
}
-
-
public CancellationTokenSource TokenSource = null;
-
private void Cancel_Click(object sender, RoutedEventArgs e)
-
{
-
if (TokenSource != null)
-
{
-
TokenSource.Cancel();
-
TokenSource = null;
-
}
-
-
}
WPF 精修篇 长时间线程加取消功能的更多相关文章
- WPF 精修篇 page
原文:WPF 精修篇 page 前言 前段时间看UML 大象 这本书 虽然马上看到了精华片 最后还是暂时暂停 因为这本书 很好 但是暂时对现在的我来说 有点超前 很多东西理解起来还是很难 但是 这本书 ...
- WPF 精修篇 WPF嵌入Winfrom控件
原文:WPF 精修篇 WPF嵌入Winfrom控件 先增加DLL 支持 使用 WindowsFormsHost 来加载Forms的控件 引用命名空间 xmlns:forms="clr-na ...
- WPF 精修篇 BackgroundWorker
原文:WPF 精修篇 BackgroundWorker 效果 <Grid> <Grid.RowDefinitions> <RowDefinition Height=&qu ...
- WPF 精修篇 非UI进程后台更新UI进程
原文:WPF 精修篇 非UI进程后台更新UI进程 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11* ...
- WPF 精修篇 多属性触发器
原文:WPF 精修篇 多属性触发器 多属性触发器就是多个属性都满足在触发 在属性触发器上加了一些逻辑判断 举栗子 这个栗子里 textBox 要满足俩个条件 才能触发背景变色 1)textbox的 ...
- WPF 精修篇 数据触发器
原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style Target ...
- WPF 精修篇 属性触发器
原文:WPF 精修篇 属性触发器 属性触发器是通过 某个条件触发改变属性 通过无代码实现功能 <Style TargetType="{x:Type Label}"> ...
- WPF 精修篇 样式继承
原文:WPF 精修篇 样式继承 这个 是新知识 样式可以继承 <Style x:Key="TextBlockStyleBase" TargetType="{x:Ty ...
- WPF 精修篇 自定义控件
原文:WPF 精修篇 自定义控件 自定义控件 因为没有办法对界面可视化编辑 所以用来很少 现在实现的是 自定义控件的 自定义属性 和自定义方法 用VS 创建自定义控件后 会自动创建 Themes 文件 ...
随机推荐
- rt-thread can
rt-thread stm32f10x-HAL can的驱动和应用.源码暂时还不支持,自己通过F4修改了一版 源码地址:https://gitee.com/gitee.thomas/rt_can rt ...
- dos转unix
方式一 # yum install dos2unix.x86_64 # dos2unix file 方式二 查看样式: :set ff? //dos/unix 设置: :set fileformat= ...
- 怎样解决非管理员账户添加Notepad++右键菜单的批处理的问题?
bat脚本如下: @echo off color 1e title 将Notepad++增加到右键菜单(或者去关联) goto :menu :menu cls echo. echo. 1 将Notep ...
- Transformer —— attention is all you need
https://www.cnblogs.com/rucwxb/p/10277217.html Transformer -- attention is all you need Transformer模 ...
- vue 开发系列(十) VUE 作用域插槽
使用场景 官方解释,有时让插槽内容能够访问子组件中才有的数据是很有用的.比如我们在使用ant-design-vue 的表格控件时. <a-table-column title="注释& ...
- linux设备驱动程序-设备树(2)-device_node转换成platform_device
设备树处理之--device_node转换成platform_device 以下讨论基于linux4.14,arm平台 platform device 设备树的产生就是为了替代driver中过多的pl ...
- 第24课经典问题(中)-----关于const对象的疑问
关于const对象的疑问const关键字能否修饰类的对象?如果可以,有什么特性?const关键字能够修饰对象const修饰的对象为只读对象只读对象的成员变量不允许被改变.(对象是只读的,成员变量不允许 ...
- 在 Oracle 中使用正则表达式
Oracle使用正则表达式离不开这4个函数: 1.regexp_like 2.regexp_substr 3.regexp_instr 4.regexp_replace 看函数名称大概就能猜到有什么用 ...
- 常用开窗函数总结(hive、sparkSQL可执行)
一:根据某个字段排序 测试数据: SQL> select * from sscore; NAME SCORE ---------- ----- aa 99 bb ...
- RST复位报文
复位报文段: 一些特殊情况,TCP一端向另一端发送复位报文,以通知对方关闭链接或者重新建立链接. 产生复位报文的三种情况: 1. 当客户端访问一个不存在的端口时,目标主机会给客户端发送一个复位报文段. ...