• 委托应用场景[摘]


    委托除了实现事件外,还能够实现很多非常有用的语言特性。
    1、Lambda 表达式。Lambda 表达式有两种存在方式,一是匿名委托,而是表达式树。
    2、匿名方法。不指定名称的委托成为匿名委托。有时候非常有用,如在绑定事件处理程序或者创建线程时。
    3、多线程同步以及跨线程操作。
    4、泛型委托。
    5、基于委托的逆变 (Contravariance) 和协变 (Covariance)。

     

    跨线程访问实例

    不允许在winform中直接跨线程访问控件
    例如:

    public partial class Form1 : Form   
        {   
            public Form1()   
            {   
                InitializeComponent();   
            }   
            private void Form1_Load(object sender, EventArgs e)   
            {   
                Thread thread = new Thread(ThreadFuntion);   
                thread.IsBackground = true;   
                thread.Start();   
            }   
            private void ThreadFuntion()   
            {   
                while (true)   
                {   
                    this.textBox1.Text = DateTime.Now.ToString();   
                    Thread.Sleep(1000);   
                }   
            }   
        }  
    

    会看到系统抛出一个异常:
    Cross-thread operation not valid:
    Control 'textBox1' accessed from a thread other than the thread it was created on .
    这是因为.net 2.0以后加强了安全机制,不允许在winform中直接跨线程访问控件的属性.
    那么怎么解决这个问题呢, 就是使用delegate和invoke来从其他线程中控制控件信息
    例如:

    public partial class Form1 : Form   
        {   
            private delegate void FlushClient();//代理   
            public Form1()   
            {   
                InitializeComponent();   
            }   
            private void Form1_Load(object sender, EventArgs e)   
            {   
                Thread thread = new Thread(CrossThreadFlush);   
                thread.IsBackground = true;   
                thread.Start();   
            }   
      
            private void CrossThreadFlush()   
            {   
                while (true)   
                {   
                    //将sleep和无限循环放在等待异步的外面   
                    Thread.Sleep(1000);   
                    ThreadFunction();   
                }   
            }   
            private void ThreadFunction()   
            {   
                if (this.textBox1.InvokeRequired)//等待异步   
                {   
                    FlushClient fc = new FlushClient(ThreadFunction);   
                    this.Invoke(fc);//通过代理调用刷新方法   
                }   
                else  
                {   
                    this.textBox1.Text = DateTime.Now.ToString();   
                }   
            }   
        }  
    
    
  • 相关阅读:
    Java学习——字符串
    Java学习——字符串
    【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies
    【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies
    Java学习——Eclipse下载,java配置,新建,输入输出
    Java学习——Eclipse下载,java配置,新建,输入输出
    python使用scikit-learn计算TF-IDF
    scanf,printf函数细节
    UVA 4855 Hyper Box
    poj 2001 Shortest Prefixes(字典树)
  • 原文地址:https://www.cnblogs.com/flyinthesky/p/1624568.html
Copyright © 2020-2023  润新知