返回> 网站首页 

[转载]Invoke封装扩展方法

yoours2015-12-29 10:14:37 阅读 1683

简介一边听听音乐,一边写写文章。

1、适用范围:
任意Control类型的控件。

2、调用
this.Label1.InvokeEx(e => e.Text = DateTime.Now);

3、代码
public static void InvokeEx<T>(this T @this, Action<T> action) where T : Control
{
    if (@this.InvokeRequired)
        @this.Invoke(action, new object[] { @this });
    else
    {
        if (!@this.IsHandleCreated)
            return;
        if (@this.IsDisposed)
            throw new ObjectDisposedException("@this is disposed.");
        action(@this);
    }
}

public static IAsyncResult BeginInvokeEx<T>(this T @this, Action<T> action) where T : Control
{
    return @this.BeginInvoke((Action)delegate { @this.InvokeEx(action); });
}

public static void EndInvokeEx<T>(this T @this, IAsyncResult result) where T : Control
{
    @this.EndInvoke(result);
}

微信小程序扫码登陆

文章评论

1683人参与,0条评论