开发者

C# extension to resolve cross thread exception

开发者 https://www.devze.com 2023-01-25 22:55 出处:网络
Is there any extension method available for resolving cross thread exception in windows forms like the one which is there for wpf forms.or any general pattern..

Is there any extension method available for resolving cross thread exception in windows forms like the one which is there for wpf forms.or any general pattern..

http://www.codeproject.com/Articles/37314/Use开发者_运维知识库ful-WPF-Threading-Extension-Method.aspx


Invoke already does some things to short-circuit here, but it would be trivial to make it complete:

public static class SyncExtensions {
    public static void InvokeIfRequired(this ISynchronizeInvoke ctrl,
            MethodInvoker method) {
        if(ctrl.InvokeRequired) ctrl.Invoke(method, null);
        else method();
    }
}

The choice of MethodInvoker is since this has specific handling inside Invoke to avoid having to use DynamicInvoke (reflection).


Did you take a look at the BeginInvoke Method?

0

精彩评论

暂无评论...
验证码 换一张
取 消