您当前的位置: 首页 >  ui

蓝不蓝编程

暂无认证

  • 0浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C# 通过线程更新UI

蓝不蓝编程 发布时间:2014-02-20 22:23:39 ,浏览量:0

摘自:http://msdn.microsoft.com/zh-cn/library/ms171728(en-us,VS.80).aspx

关键代码(form中增加):

delegate void SetTextCallback(string text);
private Thread demoThread = null;
private void setTextSafeBtn_Click(
			object sender, 
			EventArgs e)
		{
			this.demoThread = 
				new Thread(new ThreadStart(this.ThreadProcSafe));

			this.demoThread.Start();
		}
private void ThreadProcSafe()
		{
			this.SetText("This text was set safely.");
		}
private void SetText(string text)
		{
			// InvokeRequired required compares the thread ID of the
			// calling thread to the thread ID of the creating thread.
			// If these threads are different, it returns true.
			if (this.textBox1.InvokeRequired)
			{	
				SetTextCallback d = new SetTextCallback(SetText);
				this.Invoke(d, new object[] { text });
			}
			else
			{
				this.textBox1.Text = text;
			}
		}

关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0391s