最方便的用法:

private void ViewMsg(string msg)
        {

/*

control.Invoke(new SetControlTextDelegate((ct, v) => { ct.Text = v; }), new object[] { control, value });
=>
control.Invoke(new Action<Control, string>((ct, v) => { ct.Text = v; }), new object[] { control, value });

*/
            this.rtb.Invoke(new Action<RichTextBox, string>((ct, v) => { ct.AppendText(v); ct.Refresh(); }),
                    new object[] { this.rtb, msg });
            this.rtb.Invoke(new Action<string>((v) => { this.rtb.AppendText(v); this.rtb.Refresh(); }),
                    msg);
        }

Because Invoke/BeginInvoke accepts Delegate (rather than a typed delegate), you need to tell the compiler what type of delegate to create ; MethodInvoker (2.0) or Action (3.5) are common choices (note they have the same signature); like so:

control.Invoke((MethodInvoker)delegate{this.Text="Hi";});

If you need to pass in parameters, then "captured variables" are the way:

string message ="Hi";
control.Invoke((MethodInvoker)delegate{this.Text= message;});

(caveat: you need to be a bit cautious if using captures async, but sync is fine - i.e. the above is fine)

Another option is to write an extension method:

public static void Invoke(thisControl control,Action action){
control.Invoke((Delegate)action);}

then:

this.Invoke(delegate{this.Text="hi";});// or simce we are using C# 3.0this.Invoke(()=>{this.Text="hi";});

You can of course do the same with BeginInvoke:

publicstaticvoidBeginInvoke(thisControl control,Action action){
control.BeginInvoke((Delegate)action);}

If you can't use C# 3.0, you could do the same with a regular instance method, presumably in a Form base-class.

C# 线程更新UI的更多相关文章

  1. 学习通过Thread+Handler实现非UI线程更新UI组件

    [Android线程机制] 出于性能考虑,Android的UI操作并不是线程安全的,这就意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为了解决这个问题,Android制定了一条简单的规则 ...

  2. 简短总结一下C#里跨线程更新UI(转)

    摘自: http://my.oschina.net/sdqxcxh/blog/53707 跨线程更新UI是写多线程程序尤其是通信类的程序经常遇到的问题,这里面主要的问题是冲突,比如数据线程想要更新UI ...

  3. (转).NET 4.5中使用Task.Run和Parallel.For()实现的C# Winform多线程任务及跨线程更新UI控件综合实例

    http://2sharings.com/2014/net-4-5-task-run-parallel-for-winform-cross-multiple-threads-update-ui-dem ...

  4. Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面

    Android应用的开发过程中需要把繁重的任务(IO,网络连接等)放到其他线程中异步执行,达到不阻塞UI的效果. 下面将由浅入深介绍Android进行异步处理的实现方法和系统底层的实现原理. 本文介绍 ...

  5. 简短总结一下C#里跨线程更新UI

    摘自: http://my.oschina.net/sdqxcxh/blog/53707 跨线程更新UI是写多线程程序尤其是通信类的程序经常遇到的问题,这里面主要的问题是冲突,比如数据线程想要更新UI ...

  6. Android子线程更新UI的方法总结

    版权声明:本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=6121280 消息机制,对于Android开发者来说,应该是非常 ...

  7. C#子线程更新UI控件的方法总结

    http://blog.csdn.net/jqncc/article/details/16342121 在winform C/S程序中经常会在子线程中更新控件的情况,桌面程序UI线程是主线程,当试图从 ...

  8. [Android学习笔记]子线程更新UI线程方法之Handler

    关于此笔记 不讨论: 1.不讨论Handler实现细节 2.不讨论android线程派发细节 讨论: 子线程如何简单的使用Handler更新UI 问题: android开发时,如何在子线程更新UI? ...

  9. 学习通过Thread+Handler实现非UI线程更新UI组件(转)

    [Android线程机制] 出于性能考虑,Android的UI操作并不是线程安全的,这就意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为了解决这个问题,Android制定了一条简单的规则 ...

  10. 安卓 异步线程更新Ui

    异步跟新UI: 1.handler+Thread(runnable):如果handler和Thread都写在了一个Java文件中,就不说了,如果runnable定义在了一个单独的类文件中,可以通过在构 ...

随机推荐

  1. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  2. linux操作系统使用3G网卡

    几个月前,opensuse13.2出了,一直手痒痒,一有空装了一个.当时在学校,一直用着校园网,也好久没有使用3G网卡.回家的时候,办了个3G网卡,结果插电脑上没有反应,以前插上去,都会提示创建一个网 ...

  3. Beyond Compare 使用介绍

    Beyond Compare 背景 平时工作中对于源代码都是使用SVN来管理,在线状态下工作的很好,但是有时候离线状态下,对于多个版本之间的代码合并就比较麻烦.尤其是涉及到多人协作时更是如此. 所以找 ...

  4. tar解压去除文件夹

    tar zxvf test.tar.gz  --strip-components 1 解压到当前目录,并去除一级目录

  5. 构造SEH来实现跳转-转载

    下面的代码出自CSDN Delphi版的一高人(kiboisme 蓝色光芒) procedure ExceptProc{ExceptionRecord,SEH,Context,DispatcherCo ...

  6. firemonkey 得到屏幕信息

    type TO_MONITOR = class hm: HMONITOR; end; function EnumMonitorsProc(hm: HMONITOR; dc: HDC; r: PRect ...

  7. Boost format

    浅尝boost之format 概述      std::string是个很不错的东东,但实际使用时基本在每个程序里都会遇到不愉快的事情:格式化字符串.我甚至由于这个原因在代码里引入平台有关的MFC,A ...

  8. UGUI-组件

    2015-06-22 UGUI 组件 Canvas 画布 The Canvas component represents the abstract space in which the UI is l ...

  9. 代码审查工具 StyleCop 的探索

    最近我们Advent Data Service (ADS) 在项目上需要按照代码规范进行代码的编写工作,以方便将来代码的阅读与维护. 但是人工检查起来容易遗漏或者格式不统一, ReSharper又是收 ...

  10. ruby的gem和boundle安装解决办法

    gem和boundle安装在国内被墙,淘宝提供了淘宝提供了RubyGems的国内镜像站点,解决办法是: 对于gem: $ gem sources --remove https://rubygems.o ...