zoukankan      html  css  js  c++  java
  • Silverlight中 非UI线程更新UI 的几种方法

    Silverlight中 非UI线程更新UI 的几种方法:Delegate, AsyncOperation,BackgroundWorker 
    首先列一下基础代码: 

    <UserControl 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        x:Class="Shareach.TestUI.UCThreadUpdate" 
        d:DesignWidth="250" d:DesignHeight="120"> 
    <StackPanel> 
    <TextBlock x:Name="txtCalc" /> 
    </StackPanel> 
    </UserControl>
    using System; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Documents; 
    using System.Windows.Ink; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Animation; 
    using System.Windows.Shapes; 
    using System.Threading; 
    namespace Shareach.TestUI 

        public partial class UCThreadUpdate: UserControl 
        { 
           public UCMsgSend() 
           { 
                  InitializeComponent(); 
                  ThreadUpdate(); 
           } 
           void ThreadUpdate() 
           { 
                 Thread thread = new Thread(new ThreadStart(DoWork)); 
                  thread.Start(); 
           } 
           void DoWork() 
           { 
                  int i=0; 
                  while(i<100){ 
                         DoShow(i);  
                  } 
           } 
        } 
    }

    DoShow的三种写法 
    1. delegate

    void DoShow(i){ 
        this.Dispatcher.BeginInvoke( 
                                delegate { 
                                       txtCalc.Text = string.format(“result “{0}”,i); 
                                }); 
    }

    2.AsyncOperation

    void DoShow(i){ 
        //这个可以写成成员变量,我这里只是为了好区分 
        System.ComponentModel.AsyncOperation asyncOper = System.ComponentModel.AsyncOperationManager.CreateOperation(null); 
        asyncOper.Post(result => 
                { 
                    txtCalc.Text = string.format(“result “{0}”,i); 
                }, null); 
    }

    3.BackgroundWorker

    Winform 也一样,

  • 相关阅读:
    如何用redis/memcache做Mysql缓存层?
    孤儿进程和僵尸进程总结
    二叉树的遍历(非递归)
    Linux进程分配内存的两种方式--brk() 和mmap()
    Hbase
    cgroup 分析之CPU和内存部分
    十道海量数据处理面试题与十个方法大总结
    快速定位性能瓶颈,检查出所有资源(CPU、内存、磁盘IO等)的利用率(utilization)、饱和度(saturation)和错误(error)度量,即USE方法
    红黑树
    tcp 两个重要窗口:滑动窗口 和 拥塞窗口
  • 原文地址:https://www.cnblogs.com/Areas/p/2193361.html
Copyright © 2011-2022 走看看