this.Invoke( ) 括号中委托的几种写法

由于invoke函数括号中的参数类型必须是委托,所以:

    以下内容转载自:https://blog.csdn.net/joyhen/article/details/47610409
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace SQL_browser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*
             * 1.MethodInvoke和Action都是方法返回类型为空的委托.
             * 2.MethodInvoker的原型定义为---> public delegate void MethodInvoker();
             */
            //Delegate d1 = (MethodInvoker)delegate { };
            //Delegate d2 = (MethodInvoker)(() => { });

            //Delegate d3 = (Action)delegate { };
            //Delegate d4 = (Action)(() => { });

            Thread t = new Thread(() =>
            {
                Enumerable.Range(1, 10).ToList().ForEach(x =>
                {
                    if (listBox1.InvokeRequired)
                    {
                        listBox1.Invoke((Action)(() =>
                        {
                            listBox1.Items.Add(string.Format("[{0}] executed 【  (Action)(() => {{ }})  】", x));
                        }));
                    }
                });
                Enumerable.Range(11, 10).ToList().ForEach(x =>
                {
                    if (listBox1.InvokeRequired)
                    {
                        listBox1.Invoke((Action)delegate
                        {
                            listBox1.Items.Add(string.Format("[{0}] executed 【  (Action)delegate {{ }}  】", x));
                        });
                    }
                });
                Enumerable.Range(21, 10).ToList().ForEach(x =>
                {
                    if (listBox1.InvokeRequired)
                    {
                        listBox1.CCInvoke(() =>     //使用lambda表达式
                        {
                            listBox1.Items.Add(string.Format("[{0}] executed 【  lambda  】", x));
                        });
                        listBox1.CCInvoke(delegate  //使用委托
                        {
                            listBox1.Items.Add(string.Format("[{0}] executed 【  delegate  】", x));
                        });
                    }
                });
            });
            t.Start();
        }
    }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            Task taskWithFactoryAndState1 = Task.Factory.StartNew<List<int>>((stateObj) =>
            {               
                List<int> ints = new List<int>();
                for (int i = 0; i < (int)stateObj; i++)
                {
                    ints.Add(i);
                }
                return ints;
            }, 100).ContinueWith(ant =>
            {                
                listBox1.DataSource = ant.Result;
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }

    public static class ControlExtend
    {
        public static void CCInvoke(this Control control, Action action)
        {
            if (control.IsDisposed)
            return;
        try
        {
            control.Invoke((Delegate)action);
        }
        catch (ObjectDisposedException ode)
        {

        }
        catch (InvalidOperationException iox)
        {

        }
        }
    }
}

注意:button2_Click的方法会阻塞UI线程,如果数据量大或者里面的方法很耗时,界面会有卡死假象,比如窗体拖不动

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值