微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#线程中安全访问控件重用委托,避免繁复的delegate,Invoke总结

C#线程中安全访问控件(重用委托,避免繁复的delegate,Invoke)总结

@H_404_2@

1.第一种,不安全,当线程过多后,timer控件和线程中同时访问窗体控件时,有时会出现界面重绘出错。

public frmMain()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossthreadCalls
=false;
}

2.避免繁复的delegate,Invoke,转载,不推荐使用

复制代码

public static class ControlCrossthreadCalls
{
    delegate void InvokeHandler();

    ///<summary>
    /// 线程安全访问控件,扩展方法 .net 3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke
     this.SafeInvoke(() =>
     {
         tsstatus.Text = one.Email + " 开始任务....";
     });
    </summary>
    //public static void SafeInvoke(this Control control,InvokeHandler handler)
    {
        if (control.Invokerequired)
        {
            control.Invoke(handler);
        }
        else
            handler();
    }

     .net2.0线程安全访问扩展方法</summary>
     ControlCrossthreadCalls.SafeInvoke(this.tsstatus,new ControlCrossthreadCalls.InvokeHandler(delegate()
        tsstatus.Text = one.Email + " 开始任务...";
     }));
    void SafeInvoke(Control control,InvokeHandler handler)
    {
        if (control.Invokerequired)
        {
            control.Invoke(handler);
        }
        else
        {
            handler();
        }
    }
}

复制代码

 3.异步最新,推荐使用

using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Threading; /// <summary> 线程中安全访问控件,避免重复的delegate,Invoke </summary> class CrossthreadCalls { void TaskDelegate(); private void InvokeMethodDelegate(Control control,TaskDelegate handler); .net2.0中线程安全访问控件扩展方法,可以获取返回值,可能还有其它问题 CrossthreadCalls.SafeInvoke(this.statusstrip1,new CrossthreadCalls.TaskDelegate(delegate() tssstatus.Text = "开始任务..."; })); CrossthreadCalls.SafeInvoke(this.rtxtChat,0)"> rtxtChat.AppendText("测试中"); 参考:http://wenku.baidu.com/view/f0b3ac4733687e21af45a9f9.html <summary> if (control.Invokerequired) { while (!control.IsHandleCreated) { if (control.disposing || control.Isdisposed) return; } IAsyncResult result = control.BeginInvoke(new InvokeMethodDelegate(SafeInvoke),new object[] { control,handler }); control.EndInvoke(result);获取委托执行结果的返回值 return; } IAsyncResult result2 = control.BeginInvoke(handler); control.EndInvoke(result2); } this.statusstrip1.SafeInvoke(() => tsstatus.Text = "开始任务...."; this.rtxtChat.SafeInvoke(() => while (!control.IsHandleCreated) { if (control.disposing || control.Isdisposed) return; } IAsyncResult result = control.BeginInvoke(new InvokeMethodDelegate(SafeInvoke),new object[] { control,handler }); control.EndInvoke(result);获取委托执行结果的返回值 return; IAsyncResult result2 = control.BeginInvoke(handler); control.EndInvoke(result2); }

复制代码

 

更正一个我发现的C#多线程安全访问控件普遍存在的问题,仅供参考,在网上搜索多线程访问控件,发现很多都是这种类似的写法

http://msdn.microsoft.com/zh-cn/library/ms171728.aspx

@H_404_255@

复制代码

    void SetText(string text)
    {
         Invokerequired required compares the thread ID of the
         calling thread to the thread ID of the creating thread.
         If these threads are different,it returns true.
        if (this.textBox1.Invokerequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d,255)">object[] { text });
        }
        else
        {
            this.textBox1.Text = text;
        }
    }

复制代码

注意红色部分,这样写几个线程同时操作时问题不是很大,但是当我几10个几100个线程频繁操作时,就出现了System.OutOfMemoryException这个异常,猜测可能是线程堵塞,同时造成cpu很高,内存成倍增长。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐