MethodInvoker和Invoker

MethodInvoker和InvokerusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Windows

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

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

今天的文章MethodInvoker和Invoker分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/12202.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注