您的位置:首页 > 编程语言 > C#

C#一个线程操作另一个线程的控件的方法(转)

2008-09-17 23:25 489 查看
C#一个线程操作另一个线程的控件的方法字体:
Using System.Threading;
Thread thread1;
Thread thread2;
Delegate void AppendStringDelegate(string str);
AppendStringDelegate appendStringDelegate;
Public Form1()
{
InitializeComponent();
appendStringDelegate=new AppendStringDelegate(AppendString);
}
Private void AppendString(string str)
{
richTextBox1.Text+=str;
}
Private void Method1()
{
While(true)
{
Thread.Sleep(100);
richTexBox1.Invoke(appendStringDelegate,”a”);
}
}
Private void Method2()
{
While(true)
{
while(true)
{
Thread.Sleep(100);
richTextBox1.Invoke(appendStringDelegate,”b”);
}
}
//启动线程
Private void buttonStart_Click(object sender, EventArgs e)
{
richTextBox1.Text=””;
thread1=new Thread(new ThreadStart(Method1));
thread2=new Thread(new ThreadStart(Method2));
thread1.Start();
thread2.Start();
}
//终止线程
Private void buttonStop_Click(object sender,EventArg e)
{
thread1.Abort();
thread2.Join();
thread2.Abort();
thread2.Join();
MessageBox.Show(“线程1,2终止成功”);
}
本文摘自:拾金者(http://www.xkde.com) 详细出处参考:http://www.xkde.com/ComContent-149-7467.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐