如何解决跨线程操作无效异常,以便我们可以完整地保留已实现的代码 [英] How to workaround the Cross-thread operation not valid exception so we can leave already implemented code intact

查看:50
本文介绍了如何解决跨线程操作无效异常,以便我们可以完整地保留已实现的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想在一个处理程序函数中更新一个 gui 小部件的值时,该函数在后台的另一个线程中调用:

When I want to update a gui widget's value in a handler function which is called in another thread under the hood:

    private void handle_PingMessage(Dictionary<string, object> msg)
    {
        string received_text = (string)msg["text"];
        label1.Text = received_text;
    }

...它抛出以下异常:

...it throws the following exception:

Additional information: Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.

如果我将用法更改为:

    private void handle_PingMessage(Dictionary<string, object> msg)
    {
        string received_text = (string)msg["text"];
        if (label1.InvokeRequired)
        {
            label1.Invoke(new MethodInvoker(delegate { label1.Text = received_text; }));
        }
        // do any other work here
    }

它按预期工作.

我是 手动处理事件触发过程如下:

                        string event_name = "event_" + key;
                        EventInfo handler_event = this.GetType().GetEvent(event_name);
                        var event_delegate = (MulticastDelegate)this.GetType().GetField(event_name, 
                            BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
                        foreach (var handler in event_delegate.GetInvocationList())
                        {
                            handler.Method.Invoke(handler.Target, new object[] { ((JObject)payload_dict[key]).ToObject<Dictionary<string, object>>() });
                        }

为了让用户以某种方式更清楚地使用代码,是否与手动事件触发部分有关:

Is there anything to do with the manual event firing part of the code in order to let user have the clearer usage somehow:

    private void handle_PingMessage(Dictionary<string, object> msg)
    {
        string received_text = (string)msg["text"];
        label1.Text = received_text;
    }

编辑

这个示例用法的完整源代码是这里.

解决方案后完全有效的提交是 此处.

Fully working commit after solution is here.

推荐答案

在对象的构造函数中获取 SynchronizationContext.Current 的值,然后在触发事件时将事件调用发布到该对象同步上下文:

In the constructor of your object grab the value of SynchronizationContext.Current and then when you fire the event post the event invocation to that synchronization context:

public class YourPingClass
{
    private SynchronizationContext syncContext;
    public YourPingClass()
    {
        syncContext = SynchronizationContext.Current ?? 
            new SynchronizationContext();
    }

    private void FireEvent()
    {
        string event_name = "event_" + key;
        EventInfo handler_event = this.GetType().GetEvent(event_name);
        var event_delegate = (MulticastDelegate)this.GetType().GetField(event_name, 
            BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
        foreach (var handler in event_delegate.GetInvocationList())
        {
            syncContext.Post(_ => handler.Method.Invoke(handler.Target, 
                new object[] { ((JObject)payload_dict[key]).ToObject<Dictionary<string, object>>() }));
        }
    }
}

如果您愿意,您可能还希望允许您的类型的用户通过方法或属性设置器显式提供同步上下文(或等效的),但如果您的用户将要从 UI 线程创建您的对象(这对于像这样的自定义 UI 组件来说是典型的)然后您可以自己捕获当前同步上下文,这样他们就不需要明确地做任何事情.

If you want, you may also want to allow the user of your type to explicitly provide a synchronization context (or equivalent) through a method or property setter, but if your users are going to be creating your object from the UI thread (which is typical for custom UI components like this) then you can just capture the current sync context yourself so they don't need to explicitly do anything.

这篇关于如何解决跨线程操作无效异常,以便我们可以完整地保留已实现的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆