C#一个线程操作另一个线程的控件的方法
2009-12-21 08:25
375 查看
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终止成功”);
}
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终止成功”);
}
相关文章推荐
- C#一个线程操作另一个线程的控件的方法(转)
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- C#在一个线程中操作另一个线程的控件
- C# .NET 在一个线程中访问另一个线程的控件 方法汇总 (转载)
- 错误:在一个线程上创建的控件不能成为在另一个线程控件的父级(处理方法)
- c#中一个窗体调用另一个窗体的控件及方法(转贴)
- Winform跨线程操作控件,采用扩展方法适配.NET 2.0,C#封装委托方法
- C# 子线程操作主窗体控件的解决方法
- C# :线程中操作多控件 (委托实现异步及同步的方法)
- C#中子线程操作主线程中窗体上控件的方法
- C# winform中一个类中如何调用另一个窗体的控件或方法
- 在一个线程中操作另一个线程的控件
- C# 子线程操作主窗体控件的解决方法
- C# 引发“线程间操作无效: 从不是创建控件“XXX”的线程访问它” 解决方法
- 在一个线程中操作另一个线程的控件
- 2017.10.20 C#跨线程操作控件的线程安全方法
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- C#中子线程操作主线程中窗体上控件的方法
- C# 线程操作 出现“从不是创建控件lstUser的线程访问它”错误的解决方法