首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > VB.NET > Modify GUI from the worker thread (C#)
【标  题】:Modify GUI from the worker thread (C#)
【关键字】:Modify,GUI,from,the,worker,thread,C#
【来  源】:http://blog.joycode.com/demonfox/archive/2006/10/05/84651.aspx

Modify GUI from the worker thread (C#)

我在这里看到的:http://devdistrict.com/CodeDetails.aspx?A=366,不知道为什么是9/15/2006才提交的文章,应该是很平常的东西了呀。

Anyway,文章很短,我简单地意译了一下:

多线程程序应该避免在一个worker thread里直接改动GUI的内容和对象(注:I believe .NET runtime will now throw an exception if you actually do that.)

Worker thread应该通过以下方式来更新GUI:

public partial class Form1 : Form { private delegate void PrintMessageDelegate(string msg); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Start new thread Thread t = new Thread(new ThreadStart(DoWork)); t.Start(); } private void DoWork() { //Marshal request to the GUI thread textBox1.Invoke(new PrintMessageDelegate(PrintMessage), new string[] {"Print this!"}); } private void PrintMessage(string msg) { lock (this) { textBox1.Text = msg; } } }
在上面的程序中,当你点击按钮时,程序生成了一个新的线程。在这个新的worker线程里,我们想修改TextBox的内容。Worker线程marshals a call to the GUI thread。但GUI进入CPU运行的时候,它就会调用marshal过来的PrintMessage函数了。
 
技巧和诀窍:使用PrincipalPermissionAttribute在业务和数据层中添加授权规则:【上一篇】
DNN WebMail Module 开发 —— 第一篇 SharpWebMail 调试篇:【下一篇】
【相关文章】
  • Configuring the Cisco PIX Firewall
  • We provide the cheapest world of warcraft gold and
  • c# 转义字符
  • C# 101 Samples 示例参考
  • 学习C#记下的和大家分享下(更新于06.10.3)
  • 本人现在在学ASP.NET(C#篇)
  • "The Complete Reference C" 读书笔记
  • 修改Atheros芯片的无线网卡为任意品牌网卡
  • 一些关于ethereal的教程
  • 学习C#记下的和大家分享下(学习的第3天)
  • 【随机文章】
  • 惠普SAN解决方案(二)
  • 经过一个周末,又到了周一上班时间
  • "追捕"中的OICQ探测技术
  • 病毒名称 格盘犯罪”(Worm.Trilissa.e)
  • 奇迹技能书掉落表~~~!@@
  • Flash实例制作:鱼翔浅底
  • Flash和Java结合开发的利器_《Java for Flash动态网站开发手札》_部分章节连载(10)
  • JScript 方法 - lbound 方法
  • WAP WML 相关问题
  • ISDN BRI配置举例
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.