zoukankan      html  css  js  c++  java
  • Winform跨线程操作界面的策略

    BeginInvoke(new ThreadStart(() => toolStripButton1.Text = "aaa"));

    1、非跨线程操作和部分跨线程get不会引发异常;

    2、跨线程set,见上面的例子。

    3、部分跨线程get,见下面的例子。

    var a = (int)EndInvoke(BeginInvoke(new Func<int>(() => toolStripComboBox1.SelectedIndex)));
    public static class UIInvokeHelper
    {
        public static void InvokeUI(this Form form, Action action)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            if (form.InvokeRequired)
            {
                form.Invoke(new Action(action), null);
            }
            else
            {
                action();
            }
        }
        public static T InvokeUI<T>(this Form form, Func<T> func)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }
            if (func == null)
            {
                throw new ArgumentNullException("func");
            }
            if (form.InvokeRequired)
            {
                return (T)form.Invoke(new Func<T>(func));
            }
            return func();
        }
    }
    public static class UIInvokeHelper
    {
        /// <summary>
        /// 适用于非耗时、无返回值的跨线程界面操作。
        /// </summary>
        /// <param name="form"></param>
        /// <param name="action"></param>
        public static void InvokeUI(this Form form, Action action)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            if (form.InvokeRequired)
            {
                form.Invoke(new Action(action));
            }
            else
            {
                action();
            }
        }
        /// <summary>
        /// 适用于有返回值的跨线程界面操作。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="form"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static T InvokeUI<T>(this Form form, Func<T> func)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }
            if (func == null)
            {
                throw new ArgumentNullException("func");
            }
            if (form.InvokeRequired)
            {
                return (T)form.Invoke(new Func<T>(func));
            }
            return func();
        }
        /// <summary>
        /// 适用于耗时、无返回值的跨线程界面操作。
        /// </summary>
        /// <param name="form"></param>
        /// <param name="action"></param>
        public static void BeginInvokeUI(this Form form, Action action)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            if (form.InvokeRequired)
            {
                form.BeginInvoke(new Action(action));
            }
            else
            {
                Task.Factory.StartNew(action);
            }
        }
    }
  • 相关阅读:
    题解【luogu1073 最优贸易】
    题解【luogu4145 上帝造题的七分钟2(花神游历各国)】
    题解【bzoj2427 [HAOI2010]软件安装】
    以后会经常用cnblogs啦!
    题解【luogu4168 [Violet]蒲公英】
    题解【bzoj1010 [HNOI2008]玩具装箱TOY】
    题解【bzoj4653 [NOI2016] 区间】
    animation渐进实现点点点等待效果
    纯css loading动效
    文字和背景渐变动效
  • 原文地址:https://www.cnblogs.com/yao2yao4/p/3180543.html
Copyright © 2011-2022 走看看