funcdispatcher.cs

来自「破解的飞信源代码」· CS 代码 · 共 73 行

CS
73
字号
namespace Imps.Client.Core
{
    using System;
    using System.Windows.Forms;

    public static class FuncDispatcher
    {
        private static Control _mainWnd;

        public static void InvokeDelegateInUiThread(Control host, Delegate method, params object[] args)
        {
            Control control;
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (((host != null) && (host.Handle != IntPtr.Zero)) && host.Visible)
            {
                control = host;
            }
            else
            {
                control = _mainWnd;
            }
            if ((control != null) && control.IsHandleCreated)
            {
                control.BeginInvoke(method, args);
            }
            else
            {
                method.DynamicInvoke(args);
            }
        }

        public static void InvokeEventHandlerInUiThread(object sender, EventHandler handler, EventArgs e)
        {
            InvokeDelegateInUiThread(null, new OnEventHandlerDelegate(FuncDispatcher.OnEventHandler), new object[] { sender, handler, e });
        }

        public static void InvokeEventHandlerInUiThread<TEventArgs>(object sender, EventHandler<TEventArgs> handler, TEventArgs e) where TEventArgs: EventArgs
        {
            InvokeDelegateInUiThread(null, new OnEventHandlerDelegate<TEventArgs>(FuncDispatcher.OnEventHandler<TEventArgs>), new object[] { sender, handler, e });
        }

        public static void InvokeRaiseEventMethodInUiThread<TEventArgs>(EventHandlerDelegate<TEventArgs> raiseMethod, TEventArgs e) where TEventArgs: EventArgs
        {
            InvokeDelegateInUiThread(null, raiseMethod, new object[] { e });
        }

        public static void OnEventHandler(object sender, EventHandler handler, EventArgs e)
        {
            if (handler != null)
            {
                handler(sender, e);
            }
        }

        public static void OnEventHandler<TEventArgs>(object sender, EventHandler<TEventArgs> handler, TEventArgs e) where TEventArgs: EventArgs
        {
            if (handler != null)
            {
                handler.Invoke(sender, e);
            }
        }

        public static void SetMainWindow(Control mainWnd)
        {
            _mainWnd = mainWnd;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?