跨线程访问UI控件,请赏鉴

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SVS.ThreadHelper
{
    public static class ThreadSafe
    {
        public static void InvokeExt<T>(this T t, Action method, params object[] args)
            where T : Control
        {
            if (t.InvokeRequired)
            {
                t.Invoke(method, args);
            }
            else
            {
                method.Method.Invoke(t, args);
            }
        }

        public static void InvokeExt<T>(this T t, Action method)
             where T : Control
        {
            if (t.InvokeRequired)
            {
                t.Invoke(method);
            }
            else
            {
                method.Method.Invoke(t, null);
            }
        }
    }
}

posted @ 2013-11-04 21:13  zhangc3  阅读(131)  评论(0编辑  收藏  举报