C# :线程中操作多控件 (委托实现异步及同步的方法)
2009-11-27 23:39
1106 查看
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
delegate void SendDelete(SendArgs args);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedIndex = 1;
this.comboBox2.SelectedIndex = 3;
Thread thread = new Thread(CrossThreadFlush);
thread.IsBackground = true;
thread.Start();
}
private void txt(SendArgs ar)
{
//根据传过来的comBox值执行一段自定义的动作。。
MessageBox.Show(ar.str1, ar.str2);
}
private void CrossThreadFlush()
{
//while (true)
//{
SendArgs args = new SendArgs();
//
//这里使用的是匿名委托
//
// this.BeginInvoke((MethodInvoker)delegate{
// args.str1 = this.comboBox1.Text;
// //
// //有几个都可以,在参数类里面加就可以了
// //
// args.str2 = this.comboBox2.Text;
// }, args
//);
////this.BeginInvoke(new SendDelete(InvokeMethod), args); //异步
this.Invoke(new SendDelete(InvokeMethod), args); //同步
txt(args);
//}
}
void InvokeMethod(SendArgs args)
{
args.str1 = this.comboBox1.Text;
//
//有几个都可以,在参数类里面加就可以了
//
args.str2 = this.comboBox2.Text;
}
}
public class SendArgs : EventArgs
{
public string str1;
public string str2;
public string str3;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
delegate void SendDelete(SendArgs args);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedIndex = 1;
this.comboBox2.SelectedIndex = 3;
Thread thread = new Thread(CrossThreadFlush);
thread.IsBackground = true;
thread.Start();
}
private void txt(SendArgs ar)
{
//根据传过来的comBox值执行一段自定义的动作。。
MessageBox.Show(ar.str1, ar.str2);
}
private void CrossThreadFlush()
{
//while (true)
//{
SendArgs args = new SendArgs();
//
//这里使用的是匿名委托
//
// this.BeginInvoke((MethodInvoker)delegate{
// args.str1 = this.comboBox1.Text;
// //
// //有几个都可以,在参数类里面加就可以了
// //
// args.str2 = this.comboBox2.Text;
// }, args
//);
////this.BeginInvoke(new SendDelete(InvokeMethod), args); //异步
this.Invoke(new SendDelete(InvokeMethod), args); //同步
txt(args);
//}
}
void InvokeMethod(SendArgs args)
{
args.str1 = this.comboBox1.Text;
//
//有几个都可以,在参数类里面加就可以了
//
args.str2 = this.comboBox2.Text;
}
}
public class SendArgs : EventArgs
{
public string str1;
public string str2;
public string str3;
}
}
相关文章推荐
- C# 在自定义线程中采用系统委托方式操作主窗体线程控件的方法
- !!c# 委托与多线程 -- 相比background来说,本方法针对当需要通过异步代理发起UI操作,而后线程中进行sleep操作的时使用
- C#构建多线程应用程序(1) —— 使用委托来自动创建次线程以实现异步方法的调用
- Winform跨线程操作控件,采用扩展方法适配.NET 2.0,C#封装委托方法
- 基于APM模式的异步实现及跨线程操作窗体或控件方法的实现示例
- C# 多线程 用委托实现异步_调用委托的BeginInvoke和EndInvoke方法
- c#非委托方式在子线程里操作UI控件(实际上运算逻辑在子线程,更新还是在主线程,解决了UI界面无响应的问题)
- C# 引发“线程间操作无效: 从不是创建控件“XXX”的线程访问它” 解决方法
- C# 子线程操作主窗体控件的解决方法
- Unity之C#——异步委托开启线程,三种方法检测结束
- C# 采用系统委托的方式处理线程内操作窗体控件
- C# 子线程操作主窗体控件的解决方法
- C# 采用系统委托的方式处理线程内操作窗体控件(转载)
- C#中子线程操作主线程中窗体上控件的方法
- C#中子线程操作主线程中窗体上控件的方法
- C# 多线程 用委托实现异步_调用委托的BeginInvoke和EndInvoke方法
- C#跨线程操作控件 通过委托处理,MSDN上又很详细用法的说明
- Delphi中多线程用Synchronize实现VCL数据同步显示 解决在线程中操作控件出现问题
- C# 采用系统委托的方式处理线程内操作窗体控件