zoukankan      html  css  js  c++  java
  • 一个多线程更新UI的帮助类,非常好用 InvokeProxy

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;
    
    namespace TestProject
    {
    
    
        public class InvokeProxy
        {
            private System.Windows.Forms.Control _Control;
            private static Dictionary<System.Windows.Forms.Control, InvokeProxy> _InvokeProxys = new Dictionary<System.Windows.Forms.Control, InvokeProxy>();
    
            protected event EventHandler<InvokeArgs> Event_Invoke;
    
            protected InvokeProxy(System.Windows.Forms.Control c, EventHandler<InvokeArgs> invokeEvent)
            {
                if ((c != null) || (invokeEvent != null))
                {
                    this._Control = c;
                    this.Event_Invoke = invokeEvent;
                }
            }
    
            private static void c_Disposed(object sender, EventArgs e)
            {
                System.Windows.Forms.Control key = sender as System.Windows.Forms.Control;
                if ((key != null) && _InvokeProxys.ContainsKey(key))
                {
                    _InvokeProxys.Remove(key);
                }
            }
    
            public static void CreateInvokeProxy(System.Windows.Forms.Control c, EventHandler<InvokeArgs> invokeEvent)
            {
                if ((((c != null) && !c.IsDisposed) && (invokeEvent != null)) && !_InvokeProxys.ContainsKey(c))
                {
                    c.Disposed += new EventHandler(InvokeProxy.c_Disposed);
                    _InvokeProxys.Add(c, new InvokeProxy(c, invokeEvent));
                }
            }
    
            protected void Do(InvokeArgs e)
            {
                if (((this._Control != null) && !this._Control.IsDisposed) && (this.Event_Invoke != null))
                {
                    if (this._Control.InvokeRequired)
                    {
                        this._Control.Invoke(this.Event_Invoke, new object[] { this._Control, e });
                    }
                    else
                    {
                        this.Event_Invoke(this._Control, e);
                    }
                }
            }
    
            public static void Do(System.Windows.Forms.Control c, InvokeArgs e)
            {
                try
                {
                    if (((c != null) && !c.IsDisposed) && _InvokeProxys.ContainsKey(c))
                    {
                        _InvokeProxys[c].Do(e);
                    }
                }
                catch (Exception exception)
                {
                    Console.Write(string.Format("Errro in InvokeProxy{0}", exception.Message));
                }
            }
    
            protected System.Windows.Forms.Control Control
            {
                get
                {
                    return this._Control;
                }
            }
        }
    
    
        public class InvokeArgs : EventArgs
        {
            private string _Type;
    
            private string _Text;
    
            private object _Content;
    
            public string Type
            {
                get
                {
                    return this._Type;
                }
                set
                {
                    this._Type = value;
                }
            }
    
            public string Text
            {
                get
                {
                    return this._Text;
                }
                set
                {
                    this._Text = value;
                }
            }
    
            public object Content
            {
                get
                {
                    return this._Content;
                }
                set
                {
                    this._Content = value;
                }
            }
    
            public InvokeArgs()
            {
            }
    
            public InvokeArgs(string type, string text, object content)
            {
                this.Type = type;
                this.Text = text;
                this.Content = content;
            }
        }
    }
  • 相关阅读:
    真人客服
    如何给Excel中的字符串添加双引号
    新车验车上牌之一整理、核对卖车方提供的新车的各项基本手续,购买车辆保险,工商验证(转)
    如何在DNN 5.0 下打包模块
    新车验车上牌之三 —— 验车、登记注册、领取牌照(转)
    新车验车上牌之二—缴纳车辆购置税(转)
    免费的windows虚拟桌面软件
    div+css布局漫谈
    几种分页算法。翻页必备
    让广告代码不再影响你的网页加载速度
  • 原文地址:https://www.cnblogs.com/Jscriptman/p/5380677.html
Copyright © 2011-2022 走看看