您的位置:首页 - 教程 - WinForm - 正文
一个多线程更新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;
        }
    }
}

评论: