从不同的线程更新界面 [英] Updating UI from a different thread

查看:114
本文介绍了从不同的线程更新界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过,但我觉得这不是正确的问道。

I know this question has been asked before, but I feel it wasn't asked correctly.

我有一个集约化经营,我想在用户界面保持响应。我读过一些职位,说后台工作是最好的一段路要走,但是,我觉得这是假设你有源$ C ​​$ C到密集型任务。

I have an intensive operation and I'd like the UI to remain responsive. I've read a few posts that say Background worker is the best way to go, however, I think this assumes you have the source code to the intensive task.

我有没有源$ C ​​$ C为图书馆,我可以检查的进展的唯一途径是附加到被解雇的事件和获取信息的方式。

I have a library that I have no source code for, the only way I can check on the progress is to attach to events that get fired and get information that way.

我在MSDN网站上看到的例子假设一个人必须源。

The example I saw on the MSDN site assumed one would have the source.

我知道怎么去进步(这是一个百分比值),通过附加的事件,但我怎么得到值回UI?

I know how to get progress (which is a percentage value) by attaching to events, but how do I get that value back to the UI?

推荐答案

以下的答案是基于我的直觉,并没有真正做到了与第三方库的测试。

The following answer is based on my gut feeling and have not actually done it a test with third party libs.


  • 呼叫您的第三方的lib code像往常一样你在一个简单的背景(不BackgroundWorker的)线程中调用。

  • 连接库组件的事件,正常的事件处理程序在code(为了更新UI)。

  • 在事件处理程序code应该是这样的:

  • Call your third party lib code as usual you call in a simple background (not BackGroundWorker) thread.
  • Attach the library components' events to normal event handlers in your code (meant to update UI).
  • In the event handler code should look like this:

private void EventHandler(object sender, DirtyEventArgs e)
{
    if (myControl.InvokeRequired)
        myControl.Invoke(new MethodInvoker(MethodToUpdateUI), e);
    else
        MethodToUpdateUI(e);
}


private void MethodToUpdateUI(object obj) 
{
    // Update UI
}


这篇关于从不同的线程更新界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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