- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
UI界面与后台线程的交互
在 C#中,从 Main()方法开始一个默认的线程,一般称之为主线程,如果在这个进行一些非常耗 CPU 的计算,那么
UI 界面就会被挂起而处于假死状态,也就是说无法和用户进行交互了,特别是要用类似进度条来实时显示一些提示
信息的时候,这种情况就显得很糟糕。如果多开一些线程来完成一些耗时的计算,那么工作线程也是无法如此更新
UI 界面中的元素的,比如直接显示一个提示信息:label1.Text=outstring,原因很简单 UI 属于默认的主线程,而线程
间是不能这样直接访问彼此的成员的。
如果要解决以上的两个问题,那么可以借助 C#中的 Delegate 和控件类中的 Invoke()方法来搞定。
这里给出的例子比较简单,主要思路是:在Main()中启动其它的线程作为后台进程,其中一个线程是实时显示当前
的时间,一个线程是显示一些随机数,这样一来三个线程同时运行,彼此通过代理来联系。
红色代码精华
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace MutliThreadedWinFormsApp
{
public class Form1 : System.Windows.Forms.Form
{
private Thread currentTimeThread = null;
private Thread randomNumbersThread = null;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox currentTime;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox randomNumbers;
private System.Windows.Forms.GroupBox threadManager;
private System.Windows.Forms.Button pause;
private System.Windows.Forms.Button stop;
private System.Windows.Forms.Button start;
private System.Windows.Forms.RadioButton manageTime;
private System.Windows.Forms.RadioButton manageNumbers;
private System.Windows.Forms.RadioButton manageBoth;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
//创建新的工作线程
currentTimeThread = new Thread(new ThreadStart(CountTime));
currentTimeThread.IsBackground = true;
randomNumbersThread = new Thread(new ThreadStart(GenerateRandomNumbers));
randomNumbersThread.IsBackground = true;
}
protected override void Dispose( bool disposing )
{
if( disposing )
您可能关注的文档
- SWRCH35K盘条冷镦表面横裂纹成因分析.pdf
- Symmetric chain decompositions of B_n and Pi_n.pdf
- Symbolic Computing with Grassman Variables.pdf
- Symmetric Instantons and Skyrme Fields.pdf
- Symmetries in physics.pdf
- System size dependence of freeze-out properties at RHIC.pdf
- synthesis of nanoscale zero valent iron supported on exfoliated graphite for removal of nitrate.pdf
- Systemic Failures’ and ‘Human Error’ in Canadian TSB Aviation Accident Reports between.pdf
- Systemic inflammation and left atrial thrombus in patients with non-rheumatic atrial fibrillation..pdf
- SystemVue 入门.pdf
- UL CIG-023 Factory Inspection Report_Chinese.pdf
- ULN2068 1.5A_50V 4路达林顿驱动电路.pdf
- UMH11NTN;中文规格书,Datasheet资料.pdf
- Uncle Tom`s Cabin.ppt
- Under preparation for submitting to the Astrophysical Journal (March 12, 1999) Active Regio.pdf
- UM多体动力学仿真2.pdf
- Understanding Homicide and Aggravated Assault.pdf
- Understanding mobile commerce end-user adoption a triangulation perspective and suggestions.pdf
- unimedia- 20130909.pdf
- Unique sink orientations of cubes.pdf
文档评论(0)