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

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

原文地址:https://www.cnblogs.com/lonelyxmas/p/12075536.html

时间: 2024-08-30 02:08:42

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

基础篇-在非UI线程中更新UI元素

个人原创,转载请注明出处: http://blog.csdn.net/supluo/article/details/ 先了解两个概念 1.UI:User Interface的缩写,用户界面的意思.你可以不恰当的理解为我们能够看到的,操作的东西:在Android中什么才称为UI呢,可以简单的理解为View及其子类等元素.这是一个不够正确的概念,只是对新手做一个简单的抛砖引玉. 2.ANR:Application Not Responding,意思是程序没有响应. 在如下情况下,Android会报出

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

原文:WPF 精修篇 数据绑定 更新通知 开始更新一点有意思的了 首先 数据绑定  其中之一 Element 绑定 看例子 <Window x:Class="WpfApplication20.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml

为什么我们可以在非UI线程中更新UI

尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 看到这样的标题--估计N多人会说我是逗比----因为很多盆友在学习Android(特别是从4.0之后开始入门的)的时候都会常看见或听到别人说我们更新UI呢要在UI线程(或者说主线程)中去更新UI,不要在子线程中更新UI,而Android官方呢也建议我们不要在非UI线程直接更新UI,为什么呢?借助Android官方的一句话来说就是:

WPF 精修篇 依赖属性

原文:WPF 精修篇 依赖属性 依赖属性使用场景 1. 希望可在样式中设置属性. 2. 希望属性支持数据绑定. 3. 希望可使用动态资源引用设置属性. 4. 希望从元素树中的父元素自动继承属性值. 5. 希望属性可进行动画处理. 6. 希望属性系统在属性系统.环境或用户执行的操作或者读取并使用样式更改了属性以前的值时报告. 7. 希望使用已建立的.WPF 进程也使用的元数据约定,例如报告更改属性值时是否要求布局系统重新编写元素的可视化对象. 依赖属性生成 PropertyMetadata 后面可

WPF 精修篇 用户控件

原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs e); public event ButtonClick OnColorsClick ; private void Button_Click(object sender, RoutedEventArgs e) { if (OnColorsClick != null) { OnColorsClick

WPF 精修篇 动态资源

原文:WPF 精修篇 动态资源 动态资源 使用 DynamicResource 关键字 静态 就是 StaticResource 原则上是 能用静态就用静态 动态会让前台界面压力很大~ 动态资源引用 就是可以在后台改变资源 显示不同的样式  资源是一样的 就看关键字用什么 效果 <Window.Resources> <LinearGradientBrush x:Key="RectFill" EndPoint="0.5,1" StartPoint=&

WPF 精修篇 调用Win32Api

原文:WPF 精修篇 调用Win32Api 栗子是 调用WIn32API 让窗口最前 后台代码 [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); // Process process = null; private void OPenButton_Click(object sender, RoutedEventArgs e) { process = new Pro

Android 非UI线程中更新UI

Android 非UI线程中更新UI runOnUiThread(new Runnable() { public void run() { onDown(null); } });

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

当在非UI线程中更新UI(程序界面)时会出现如下图所示的异常: 那如何才能在非UI线程中更细UI呢? 方法有很多种,在这里主要介绍两种: 第一种:在需要更新UI的代码行后加Looper.prepare();与Looper.loop();两句话即可.如: new Thread(){ @Override public void run() { // TODO Auto-generated method stub txtRotation.setText("在非UI线程中更新UI!"); Lo