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();
// }
//}
解决方案 »
- 请教个内存写法
- 如何通过程序对iis进行操作?
- 向SP1234大哥问个物联网性能问题~~
- 跪求五子棋的悔棋问题
- DataSet中DataTable重复行删除和求和问题
- 请教自定义的控件如何在代码中动态创建?
- 请教如何进行DLL转换
- 属性“nowrap”被视为已过时。建议使用较新的构造
- 调试的时候出现这个错误:“命名空间不直接包含诸如字段或方法之类的成员”,请各位大侠帮帮忙,告诉我原因!
- windows FORM 中如何插入flash?并且可以互传变量?
- 想做一个项模板,这个模板能够有一个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();
}
}