MethodInvoker和Invoker

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)
        {

        }
        }
    }
}
</int></int></int>
MethodInvoker和Invoker_第1张图片

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




你可能感兴趣的:(invoker,MethodInvoker)