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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 易学

Control Panel Shortcuts 基于LMS8962的跑马灯教学程序——定时器、串口及GPIO的使用 使用SerialPort及ZedGraph快速实现串口数据实时显示 基于.NET Compact Framework的实时曲线绘制控件 在Windows mobile 6.0模拟器中实现蓝牙数据采集 Firmata解析 定制带有模拟器且支持ActiveSync调试的WINCE5.0 SDK WinCE下动态显示绘图控件 XPE及CE系统对比 西溪湿地(摘录) - 易学 - 博客园 当前电子鼻系统数据处理中常用的模式识别技术 各大公司样片申请指南 数字万用表集成块的代换技巧 AD855x系列在微弱信号检测中的应用 测试Live Writer能否直接发图片 Radial 射线 昆明理工大学博士、硕士论文撰写规范 “共轭变换”图像处理算法在FPGA 上实现的研究
多线程环境下的UI异步操作
易学 · 2010-04-01 · via 博客园 - 易学

解决VS2005中,线程间不可互操作的问题,一揽子解决方案:

一、首先,定义一个类:SetControlProperty

using System.Reflection;
using System;
using System.Windows.Forms;
namespace Ways.Utils
{
    class SetControlProperty
    {
        delegate void SetValueDelegate(Object obj, Object val, Object[] index);
        public SetControlProperty(Control ctrl, String propName, Object val)
        {
            PropertyInfo propInfo = ctrl.GetType().GetProperty(propName);
            Delegate dgtSetValue = new SetValueDelegate(propInfo.SetValue);
            ctrl.Invoke(dgtSetValue, new Object[3] { ctrl, val, null });
        }
    }
}

二、在要操作Form中调用

本例中,此调用是由一通讯事件引发的:

void testcommand_EndStatusEvent(object sender, EventArgs e)
{
    new SetControlProperty(label4, "Text", "END");
    new SetControlProperty(button1, "Enabled", true);
} 

三、 最简化,但却不安全的方案

Control.CheckForIllegalCrossThreadCalls = false;

试过了,在.net compact framework中,不可用!

四、.NETCF中的解决方案,来源:.NET Compact Framework 多线程环境下的UI异步刷新

在进行WinCe或者Windows Mobile开发中,通常需要把一些任务提交给工作线程(Worker Thread)完成,当worker thread 线程发生状态变更的时候需要通知UI进程刷新UI,比如一个网络连接程序,Worker Thread线程负责管理WiFi,GPRS或者3G等连接,当连接状态发生改变时候,Worker Thread把更新状态通知UI Thread,而UI Thread更新UI通知用户。

这里常常有个疑问,为什么Worker Thread不直接更新UI,这样更简单直接和明了。但是UI刷新不是线程安全(Thread Safe)的,所以Worker Thread直接更新UI会抛出"cross-thread operation not valid"异常。所以需要Thread Safe的通知方法,下面演示更新短语(Message)的方法如下:
Worker Thread Class

class ConnectionMgr
{
    //Delegate for Message
    public delegate void MessageEventHandler(string msg);
    public event MessageEventHandler MessageEvent; 
    //the delegate of Message event
    private void MessageHandler(string msg)
    {
        MessageEventHandler messageEvent = MessageEvent;
        if (messageEvent != null)
        {
            messageEvent(msg);
        }
    }
    private void ConnectHandler()
    {
        MessageHandler("Connected");
    }
    private void DisconnectHandler()
    {
        MessageHandler("Disconnected");
    }
} 

在Worker Thread定义delegate和event供UI Thread注册。当状态发生改变是调用该delegate。

UI Thread

public partial class Form1 : Form
{
    public Form1()
    {
        //register the connect event
        ConnectionMgr.Instance.MessageEvent += MessageEvent;
    } 
    private void MessageEvent(string msg)
    {
        SafeWinFormsThreadDelegate d = new SafeWinFormsThreadDelegate(ShowMessage);
        Invoke(d, new object[] { msg} ); 
    } 
    public delegate void SafeWinFormsThreadDelegate(string msg);
    private void ShowMessage(string msg)
    {
        eventText.Text = msg;
    }
} 

UI Thread通过delegate订阅连接事件,当连接状态发生改变的时候,Worker Thread异步调用void MessageEvent(string msg)。这里调用Invoke方法来进行线程安全的调用。调用参数使用Object[]来传递,因此程序可以传递任何信息,UI可以呈现任何信息只要Worker Thread能提供。

参考文献Control.Invoke Method  in MSDN