我眼中委托的真正面貌(三)
2009-05-14 08:28:38 来源:WEB开发网using System.Threading;
namespace MulTrdDelegate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//定义委托类型
public delegate void TreadDelgate();
//初始化子线程对象
private Thread demoThread = null;
private void button1_Click(object sender, EventArgs e)
{
demoThread = new Thread(new ThreadStart(ThreadProcUnsafe));
demoThread.Start();
}
public void ThreadProcUnsafe()
{
if (this.textBox1.InvokeRequired)
{
TreadDelgate Objdelegate = new TreadDelgate(SetText);
this.Invoke(Objdelegate, new object[] {});
MessageBox.show("委托方法已返回!");
}
else
{
SetText();
}
}
private void SetText()
{
Thread.sleep(5000);//人为延长方法的执行时间
string text = "这个控件的内容是由子线程实现的";
this.textBox1.Text = text;
}
}
}
更多精彩
赞助商链接