C# 多线程 - 在线程之间移动对象

标签 c# winforms multithreading

我正在使用一个 winforms 控件,它既是一个 GUI 元素,也进行一些尚未向开发人员公开的内部处理。当这个组件被实例化时,它可能需要 5 到 15 秒才能准备好,所以我想做的是把它放在另一个线程上,当它完成后将它带回 gui 线程并将它放在我的表单上。问题是这将(并且已经)导致跨线程异常。

通常,当我使用工作线程时,它只使用简单的数据对象,我可以在处理完成后返回,然后使用主线程上已有的控件,但我从来不需要以这种方式移动整个控件。

有谁知道这是否可行,如果可行怎么办?如果不是,如何处理这样一个有可能锁定主图形用户界面的问题?

最佳答案

不需要锁定GUI,只需要调用invoke:

Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control. ref

代码如下:

public delegate void ComponentReadyDelegate(YourComponent component);
public void LoadComponent(YourComponent component)
{
    if (this.InvokeRequired)
    {
        ComponentReadyDelegate e = new ComponentReadyDelegate(LoadComponent);
        this.BeginInvoke(e, new object[]{component});
    }
    else
    {
        // The component is used by a UI control
        component.DoSomething();
        component.GetSomething();
    }
}

// From the other thread just initialize the component
// and call the LoadComponent method on the GUI.
component.Initialize(); // 5-15 seconds
yourForm.LoadComponent(component);

通常从另一个线程调用 LoadComponent 会导致跨线程异常,但通过上述实现,该方法将在 GUI 线程上调用。

InvokeRequired 告诉您是否:

the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on. ref

更新:
因此,如果我理解正确的话,控制对象是在 GUI 线程以外的线程上创建的,因此即使您能够将它传递给 GUI 线程,您仍然无法在不导致跨线程异常的情况下使用它.解决方案是在 GUI 线程上创建对象,但在单独的线程上初始化它:

public partial class MyForm : Form
{
    public delegate void ComponentReadyDelegate(YourComponent component);
    private YourComponent  _component;
    public MyForm()
    {
        InitializeComponent();
        // The componet is created on the same thread as the GUI
        _component = new YourComponent();

        ThreadPool.QueueUserWorkItem(o =>
        {
            // The initialization takes 5-10 seconds
            // so just initialize the component in separate thread
            _component.Initialize();

            LoadComponent(_component);
        });
    }

    public void LoadComponent(YourComponent component)
    {
        if (this.InvokeRequired)
        {
            ComponentReadyDelegate e = new ComponentReadyDelegate(LoadComponent);
            this.BeginInvoke(e, new object[]{component});
        }
        else
        {
            // The component is used by a UI control
            component.DoSomething();
            component.GetSomething();
        }
    }
}

关于C# 多线程 - 在线程之间移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2954089/

相关文章:

c# - 在 datagridview 中获取选定行的值并将它们相加

c# - 值类型扩展方法是否提供对原始值的写访问?

c# - 将 JSON 表示为 C# 类

c# - 双击定时器事件

c++ - 1 个线程工作但导致调用方法不返回的线程问题

java - Java多线程奇怪的问题。某些线程自行关闭?

c - 如何在C/Cilk中以线程安全的方式打印?

c# - 如何防范 "Object cannot be cast from DBNull to other types"?

c# - 在 C# 中嵌入 Google map

c# - 如何保护 WinForms 应用程序内部的 HTTP 流量免受嗅探