zoukankan      html  css  js  c++  java
  • Control的Invoke和BeginInvoke

    1.Control.Invoke

    Invoke 会导致线程的阻塞,但是是顺序执行的,

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add("begin");
        listBox1.Invoke(new Action(() => { listBox1.Items.Add("invoke"); }));
        listBox1.Items.Add("after");
    }

    2.Control.BeginInvoke

    BeginInvoke 同样会导致线程的阻塞,在执行完主线程(UI线程)后才会执行,

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add("begin");
        listBox1.BeginInvoke(new Action(() => { listBox1.Items.Add("begininvoke"); }));
        listBox1.Items.Add("after");
    }

    若想要在线程执行结束之前执行 BeginInvoke,可以使用 EndInvoke,

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add("begin");
        var i = listBox1.BeginInvoke(new Action(() => { listBox1.Items.Add("begininvoke"); }));
        listBox1.EndInvoke(i);
        listBox1.Items.Add("after");
    }

    或者 BeginInvoke 会在一个 Invoke 调用前执行,

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add("begin");
        listBox1.BeginInvoke(new Action(() => { listBox1.Items.Add("begininvoke"); }));
        listBox1.Invoke(new Action(() => { listBox1.Items.Add("invoke"); }));
        listBox1.Items.Add("after");
    }

    Tips:

    如果 Invoke 在支线程中定义调用,那么它同样会在主线程(UI线程)中执行,也会阻塞主线程和支线程;

    如果 BeginInvoke 在支线程中定义调用,那么它也会在主线程(UI线程)中执行,也会阻塞主线程,但相对于支线程是异步的。

    Invoke 和 BeginInvoke 的主要作用是在支线程中访问 UI 线程

    private System.Timers.Timer myTimer;
    
    private void Form1_Load(object sender, EventArgs e)
    {
        //窗体打开时创建定时器,并设置执行频率时间,会创建新线程
        this.myTimer = new System.Timers.Timer(1000);
        //设置任务
        this.myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
        this.myTimer.AutoReset = true;
        this.myTimer.Enabled = true;
        this.myTimer.Start();
    }
    
    private void myTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        count++;
        label1.Invoke(new Action(() => { label1.Text = count.ToString() + "s"; }));
    }

    What's up with BeginInvoke?

  • 相关阅读:
    bzoj 1853: [Scoi2010]幸运数字 容斥
    bzoj 3545&&3551: [ONTAK2010]Peaks &&加强版 平衡树&&并查集合并树&&主席树
    bzoj 2331: [SCOI2011]地板 插头DP
    bzoj 3669: [Noi2014]魔法森林 动态树
    bzoj 2734: [HNOI2012]集合选数 状压DP
    bzoj 3751: [NOIP2014]解方程 同余系枚举
    bzoj 2594: [Wc2006]水管局长数据加强版 动态树
    bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树
    bzoj 2209: [Jsoi2011]括号序列 splay
    bzoj 1223: [HNOI2002]Kathy函数 数位DP 高精度
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/9963565.html
Copyright © 2011-2022 走看看