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;
            }
        }
    }
  • 相关阅读:
    this指向问题
    原生js实现的金山打字小游戏(实例代码详解)
    js实现点赞效果
    .net core部署到linux可能碰到的问题
    Linux curl命令详解 Web程序
    用十年来学编程
    JAVA的字符串拼接与性能
    PHP学习的技巧和学习的要素总结
    php实现验证邮箱格式的代码实例
    PHP页面中文乱码处理办法
  • 原文地址:https://www.cnblogs.com/Jscriptman/p/5380677.html
Copyright © 2011-2022 走看看