原文:WPF 精修篇 非UI进程后台更新UI进程


  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. </StackPanel>
  13. <StackPanel Margin="0" Grid.Row="1">
  14. <TextBlock x:Name="odd" TextWrapping="Wrap" Text="奇数数量:"/>
  15. <TextBlock x:Name="even" TextWrapping="Wrap" Text="偶数数量:"/>
  16. </StackPanel>
  17. </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 (i % 2 == 0)
  8. {
  9. evencount++;
  10. }
  11. else
  12. {
  13. oddcount++;
  14. }
  15. }
  16. }
  17. private void Button_Click(object sender, RoutedEventArgs e)
  18. {
  19. int from=0;
  20. int to = 0;
  21. if(int.TryParse(beginText.Text,out from)&&int.TryParse(endText.Text,out to) )
  22. {
  23. button.IsEnabled = false;
  24. ThreadPool.QueueUserWorkItem(_ =>
  25. {
  26. Make(from, to);
  27. Dispatcher.BeginInvoke(new Action(() =>
  28. {
  29. odd.Text = "奇数数量:" + oddcount;
  30. even.Text = "偶数数量:" + evencount;
  31. button.IsEnabled = true;
  32. }));
  33. });
  34. }
  35. }

  1. Dispatcher.BeginInvoke(new Action(() =>
  2. {
  3. //UI线程
  4. }));

WPF 精修篇 非UI进程后台更新UI进程的更多相关文章

  1. WPF 精修篇 数据绑定 更新通知

    原文:WPF 精修篇 数据绑定 更新通知 开始更新一点有意思的了 首先 数据绑定  其中之一 Element 绑定 看例子 <Window x:Class="WpfApplicatio ...

  2. WPF 精修篇 用户控件

    原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...

  3. WPF 精修篇 依赖属性

    原文:WPF 精修篇 依赖属性 依赖属性使用场景 1. 希望可在样式中设置属性. 2. 希望属性支持数据绑定. 3. 希望可使用动态资源引用设置属性. 4. 希望从元素树中的父元素自动继承属性值. 5 ...

  4. WPF 精修篇 WPF 使用ActiveX

    原文:WPF 精修篇 WPF 使用ActiveX WPF 实现远程桌面功能 首先使用 开发人员命令提示 进入 自己的项目文件根目录下 输入 aximp C:\windows\System32\msts ...

  5. WPF 精修篇 调用Win32Api

    原文:WPF 精修篇 调用Win32Api 栗子是 调用WIn32API 让窗口最前 后台代码 [DllImport("user32.dll")] private static e ...

  6. WPF 精修篇 动态资源

    原文:WPF 精修篇 动态资源 动态资源 使用 DynamicResource 关键字 静态 就是 StaticResource 原则上是 能用静态就用静态 动态会让前台界面压力很大~ 动态资源引用 ...

  7. Android开之在非UI线程中更新UI

    当在非UI线程中更新UI(程序界面)时会出现例如以下图所看到的的异常: 那怎样才干在非UI线程中更细UI呢? 方法有非常多种.在这里主要介绍三种: 第一种:调用主线程mHandler的post(Run ...

  8. WPF 精修篇 数据触发器

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

  9. WPF 精修篇 属性触发器

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

随机推荐

  1. 深度学习-TensorFlow2.0笔记(一)

    一.Tensor 1.1 什么是Tensor?Tensor的数据类型 Tensor是张量的意思,在TensorFlow中张量可以是标量(scalar).向量(vector).矩阵(matrix).高维 ...

  2. The listener supports no services oracle注册监听

    问题登场: [oracle@my-e450 ~]$ lsnrctl status …… The listener supports no servicesThe command completed s ...

  3. odoo10学习笔记十三:qweb报表

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189336.html 一:概述 报表是使用qweb定义的,报表的pdf导出是使用wkhtmltopdf来完 ...

  4. 重写jquery ajax 方法

    方法一 // TODO 主要功能为重写ajax $.ajaxSetup({ cache: false, headers: { "xxxxx": "xxxxx" ...

  5. 【BZOJ3716】[PA2014]Muzeum(贪心+网络流)

    BZOJ 题意: 在二维网格图中有\(n\)个物品,每个物品有价值:但有\(m\)个警卫看管这些物品,每个警卫面朝\(y\)轴负方向,能看到一定角度(假定能够看到无穷远). 现在每个敬畏有一个贿赂价钱 ...

  6. UiPath:Split(","c)以逗号区分遍历字符串数组

    学习中遇到同一用户多种职业的情况,因为所有的数据都是从Excel里面取的,所以只能把所有的职业写在一个单元格里,以逗号区分. 那么就需要先把字符串转为数组,然后遍历数组去添加职位.如图 Split(& ...

  7. 【Spring IoC】依赖注入DI(四)

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法.通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由程 ...

  8. 第02组 Beta冲刺(4/4)

    队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:了解整个游戏的流程 GitHub签入记录 接下来的计划:继续完成游戏 还剩下哪些任务:完成游戏 燃尽图 遇到的困难:没有美术比较好的 ...

  9. [LeetCode] 457. Circular Array Loop 环形数组循环

    You are given a circular array nums of positive and negative integers. If a number k at an index is ...

  10. Spring Cloud Gateway的全局异常处理

    Spring Cloud Gateway中的全局异常处理不能直接用@ControllerAdvice来处理,通过跟踪异常信息的抛出,找到对应的源码,自定义一些处理逻辑来符合业务的需求. 网关都是给接口 ...