一个多线程更新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;
        }
    }
}
复制代码

 

posted @   mild  阅读(411)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示