1.第一种,不安全,时间控件和线程同时访问窗体控件时,有时会出现界面重绘出错。
public frmMain()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}2.避免繁复的delegate,Invoke,推荐,安全/// <summary>
/// 线程中安全访问控件,避免繁复的delegate,Invoke
/// </summary>
public static class ControlCrossThreadCalls
{
public delegate void InvokeHandler(); /// <summary>
/// .net2.0线程中安全访问控件扩展方法-事理
/// </summary>
/// ControlCrossThreadCalls.SafeInvoke(this,this.statusStrip1, new ControlCrossThreadCalls.InvokeHandler(delegate()
/// {
/// tssStatus.Text = "开始任务...";
/// }));
/// ControlCrossThreadCalls.SafeInvoke(this,this.rtxtChat, new ControlCrossThreadCalls.InvokeHandler(delegate()
/// {
/// rtxtChat.AppendText("测试中");
/// }));
/// 参考:http://wenku.baidu.com/view/f0b3ac4733687e21af45a9f9.html
/// <summary>
public static void SafeInvoke(Form form, Control control, InvokeHandler handler)
{
if (control.InvokeRequired)
{
form.Invoke(handler);
}
else
{
handler();
}
} /// <summary>
/// 线程安全访问控件,扩展方法.net3.5用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke
/// this.statusStrip1.SafeInvoke(() =>
/// {
/// tsStatus.Text = "开始任务....";
/// });
/// this.rtxtChat.SafeInvoke(() =>
/// {
/// rtxtChat.AppendText("测试中");
/// });
/// </summary>
//public static void SafeInvoke(this Control control, InvokeHandler handler)
//{
// if (control.InvokeRequired)
// {
// control.Invoke(handler);
// }
// else
// {
// handler();
// }
//}
public frmMain()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}2.避免繁复的delegate,Invoke,推荐,安全/// <summary>
/// 线程中安全访问控件,避免繁复的delegate,Invoke
/// </summary>
public static class ControlCrossThreadCalls
{
public delegate void InvokeHandler(); /// <summary>
/// .net2.0线程中安全访问控件扩展方法-事理
/// </summary>
/// ControlCrossThreadCalls.SafeInvoke(this,this.statusStrip1, new ControlCrossThreadCalls.InvokeHandler(delegate()
/// {
/// tssStatus.Text = "开始任务...";
/// }));
/// ControlCrossThreadCalls.SafeInvoke(this,this.rtxtChat, new ControlCrossThreadCalls.InvokeHandler(delegate()
/// {
/// rtxtChat.AppendText("测试中");
/// }));
/// 参考:http://wenku.baidu.com/view/f0b3ac4733687e21af45a9f9.html
/// <summary>
public static void SafeInvoke(Form form, Control control, InvokeHandler handler)
{
if (control.InvokeRequired)
{
form.Invoke(handler);
}
else
{
handler();
}
} /// <summary>
/// 线程安全访问控件,扩展方法.net3.5用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke
/// this.statusStrip1.SafeInvoke(() =>
/// {
/// tsStatus.Text = "开始任务....";
/// });
/// this.rtxtChat.SafeInvoke(() =>
/// {
/// rtxtChat.AppendText("测试中");
/// });
/// </summary>
//public static void SafeInvoke(this Control control, InvokeHandler handler)
//{
// if (control.InvokeRequired)
// {
// control.Invoke(handler);
// }
// else
// {
// handler();
// }
//}
解决方案 »
- 关于c#的作用域问题
- 如何得到MenuItem的名字进行判断
- 在winform中怎么绑定listview ??
- 为什么.net版的回答问题的人都把提问的人当白痴!!!!
- 遇到一个pictureBox问题
- 如何获取当前webbrowser控件中所浏览网页的cookies呢?
- xlFancyPlay媒体播放器,采用DirectX驱动,用C#编写(测试有分)
- 学习过.Net的各位高手,请你们推荐一些.net比较经典的书。。。。。
- 手机短信,如何解决中文短信发送
- 我用Microsoft Office Chart 10.0组件,为什么用着用着连不上数据库了,连连数据库的选项都变为不可用状态?
- 想做一个项模板,这个模板能够有一个custom tool让这个项(一个xml文件)可以生成cs
- 为何ListView控件中ItemTemplate的 编辑 按钮点击时无效
/// <summary>
/// .net2.0线程中安全访问控件扩展方法-有问题,但是可以获取返回值
/// </summary>
/// CrossThreadCalls.SafeInvoke(this,this.statusStrip1, new CrossThreadCalls.InvokeHandler(delegate()
/// {
/// tssStatus.Text = "开始任务...";
/// }));
/// CrossThreadCalls.SafeInvoke(this,this.rtxtChat, new CrossThreadCalls.InvokeHandler(delegate()
/// {
/// rtxtChat.AppendText("测试中");
/// }));
/// 参考:http://wenku.baidu.com/view/f0b3ac4733687e21af45a9f9.html
/// <summary>
public static void SafeInvoke(Control control, InvokeHandler handler)
{
if (control.InvokeRequired)
{
control.Invoke(handler);
}
else
{
handler();
}
}