移动线程之间的对象

移动线程之间的对象

本文介绍了C#多threading-移动线程之间的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与一个WinForms控制,既在GUI元素,也不会尚未暴露于显影剂一些内部处理工作。当这个组件实例化可能需要5秒至15准备就绪,所以我想要做的是把它在另一个线程,当它做了把它带回了GUI线程,并把它的窗体上。问题是,这将(并拥有)造成一个跨线程异常。

i am working with a winforms control that is both a GUI element and also does some internal processing that has not been exposed to the developer. When this component is instantiated it may take between 5 and 15 seconds to become ready so what i want to do is put it on another thread and when its done bring it back to the gui thread and place it on my form. The problem is that this will (and has) cause a cross thread exception.

通常,当我与工人的工作线程它只是简单的数据对象,我可以推后处理时完成后再用控件已经使用在主线程,但我从来没有需要移动以这种方式整个控制。

Normally when i work with worker threads its just with simple data objects i can push back when processing is complete and then use with controls already on the main thread but ive never needed to move an entire control in this fashion.

有谁知道这是可能的,如果是这样怎么样?如果不是如何来处理这样一个问题,即有锁定主界面的潜力?

Does anyone know if this is possible and if so how? If not how does one deal with a problem like this where there is the potential to lock the main gui?

推荐答案

您别吨需要锁定GUI,你只需要调用调用:

You don't need to lock the GUI, you just need to call invoke:

控件绑定到
A特定线程和是不是线程
安全。因此,如果要调用
控制的从不同的
线的方法,你必须使用的
控件的调用方法之一元帅
调用正确的线程。这种
属性可以用来确定
你必须调用Invoke方法,
可以是有用的,如果你不知道什么
线程拥有控制权。

下面是如何看起来代码:

Here is how it looks in code:

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线程上调用

Normally calling the LoadComponent from another thread will cause a cross-thread exception, but with the above implementation the method will be invoked on the GUI thread.

InvokeRequired 告诉你,如果:

调用者必须调用的制作方法时Invoke方法
调用到
控制,因为来电者是在
不同的线程比
控制上创建一个。

更新:结果
所以,如果我理解正确,你是在创建控制对象线程不是GUI线程之外的,所以即使你能够把它传递给GUI线程你仍然不能使用它,而不会造成一个跨线程异常。该解决方案是创建GUI线程上的对象,但初始化它放在一个单独的线程:

Update:
So if I understand you correctly the control object is created on a thread other than the GUI thread, therefore even if you were able to pass it to the GUI thread you still won't be able to use it without causing a cross-thread exception. The solution would be to create the object on the GUI thread, but initialize it on a separate thread:

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#多threading-移动线程之间的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:26