惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - xqiwei

WCF RIA Services DomainService life-cycle and adding Transactions Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) looping(and modifying) a collection - xqiwei LineBreak in a tooltip in xaml - xqiwei GetObjectbyKey in E.F. vs. Querying for a single entity Use GetObjectByKey() for better performance Visual Studio集成开发环境无法启动调试 C#中关于String.Equals(object,object)和(object==object )的比较 - xqiwei - 博客园 IValueConverter 接口 ASP.NET页面生命周期和asp.net应用程序生命周期 Windows Presentation Foundation Tools and Controls ArcEngine开发之Command控件使用篇 Resharper进阶 C# 中的委托和事件 WPF系列文章 新技术文章 - xqiwei flash与javascript、asp.net(数据库)的交互 使用vs.net ajax实现幻灯片的效果 职业规划
C#多线程 使用委托更新UI实例(WP7开发 其他线程中更新UI)(转载)
xqiwei · 2012-02-16 · via 博客园 - xqiwei

在C#中,非主线程(即非UI线程,就是通过new Thread创建的线程)是不能直接操作UI元素的,Android中也一样,必须通过Handler与UI线程通讯,通知UI线程更新.而C#则采用委托的方式更新UI.下面用一个简单的实例来说明.这是一个WPF项目,只有一个名为textBlock1的TextBlock用于显示,一个名为button1的Button用于开启新线程来更新textBlock1.新线程执行的内容是0-100循环,而让textBlock1显示当前循环的进度.Windows Phone 7开发中,也通用,参考
上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
 
namespace WpfApplication2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        delegate void DelegateTest(int i);
        //使用delegate关键字,声明委托类型,参数和返回值都有要求,见下面说明
        DelegateTest delegateTest;
        //声明委托类型所对应的委托
        public MainWindow()
        {
            InitializeComponent();
        }
 
 
        public void SetText(int i)
        {
            //更新UI的方法
            textBlock1.Text = i.ToString();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            delegateTest = SetText;
            //实例化委托,就是添加委托要执行的方法,
            //可以用+=添加多个方法,如下面再增加 delegateTest +=方法名;
            //添加的方法,返回值和参数列表 必须与委托类型结构相同
            //即这里DelegateTest与SetText参数都为int,返回值都为void
 
            Thread t = new Thread(new ThreadStart(ThreadProc));
            //开线程方法与Java有些不同
            t.Start();  
            //执行一个新线程
 
        }
        public void ThreadProc()
        {
            for (int i = 0; i < 100; i++)
            {
                this.Dispatcher.BeginInvoke(delegateTest, i);
                //创建Dispatcher,使用BeginInvoke方法进行委托
                Thread.Sleep(1000);
                //睡眠1秒
            }
 
        }
    }
}