如何从Work线程调用更新UI线程?

时间:2021-04-24 20:56:51

If i have a function in a thread that is processing some data, then it calls a callback function to update the status in the UI.

如果我在处理某些数据的线程中有一个函数,那么它会调用一个回调函数来更新UI中的状态。

If the UI takes time to process the callback function then it is not so much usefull. Instead of just handling the update in the callback function, should it send some kind of message to the UI that way it doesnt block?

如果UI需要时间来处理回调函数,那么它就没那么有用了。它应该以不会阻止的方式向UI发送某种消息,而不仅仅是在回调函数中处理更新吗?

I mean when in the processing function and i call the update status function, this should return immediately to the procesing function, then in the update it can wait all it wants for the UI update to finish.

我的意思是当在处理函数中并且我调用更新状态函数时,它应该立即返回到处理函数,然后在更新中它可以等待所有想要的UI更新完成。

Or do I need a 3rd thread to handle the sending update data to the UI ?

或者我是否需要第三个线程来处理向UI发送更新数据?

3 个解决方案

#1


2  

Usually there's a way of posting the callback to the UI thread without blocking.

通常有一种方法可以将回调发布到UI线程而不会阻塞。

For instance:

I'm sure if you look at the docs for the UI toolkit you're using, you'll find something similar.

我敢肯定,如果你查看你正在使用的UI工具包的文档,你会发现类似的东西。

#2


2  

In .NET (WinForms, WPF, Silverlight) you just need to use the Dispatcher object on the Thread of the UI to call the update method for the User Interface. Dispatchers can be called either synchronously (using Invoke) either async (using BeginInvoke/EndInvoke). Please note that in .NET there is a requirement to call EndInvoke for each BeginInvoke (becuase .NET doesn't gives you the warranty that the async handles will be freed), so Fire and Forget isn't an option by default (unless you implement your own FireAndForget)

在.NET(WinForms,WPF,Silverlight)中,您只需要在UI的Thread上使用Dispatcher对象来调用用户界面的更新方法。调度程序可以同步调用(使用Invoke)async(使用BeginInvoke / EndInvoke)。请注意,在.NET中,需要为每个BeginInvoke调用EndInvoke(因为.NET不会为您提供将释放异步句柄的保证),因此默认情况下Fire和Forget不是一个选项(除非您实现自己的FireAndForget)

#3


0  

Mapad posted a link to on UI and threads here which you may find useful. You didn't mention which UI toolkit and which language so I can't give you any specifics.

Mapad在这里发布了一个关于UI和线程的链接,您可能会觉得这些链接很有用。你没有提到哪个UI工具包和哪种语言,所以我不能给你任何细节。

#1


2  

Usually there's a way of posting the callback to the UI thread without blocking.

通常有一种方法可以将回调发布到UI线程而不会阻塞。

For instance:

I'm sure if you look at the docs for the UI toolkit you're using, you'll find something similar.

我敢肯定,如果你查看你正在使用的UI工具包的文档,你会发现类似的东西。

#2


2  

In .NET (WinForms, WPF, Silverlight) you just need to use the Dispatcher object on the Thread of the UI to call the update method for the User Interface. Dispatchers can be called either synchronously (using Invoke) either async (using BeginInvoke/EndInvoke). Please note that in .NET there is a requirement to call EndInvoke for each BeginInvoke (becuase .NET doesn't gives you the warranty that the async handles will be freed), so Fire and Forget isn't an option by default (unless you implement your own FireAndForget)

在.NET(WinForms,WPF,Silverlight)中,您只需要在UI的Thread上使用Dispatcher对象来调用用户界面的更新方法。调度程序可以同步调用(使用Invoke)async(使用BeginInvoke / EndInvoke)。请注意,在.NET中,需要为每个BeginInvoke调用EndInvoke(因为.NET不会为您提供将释放异步句柄的保证),因此默认情况下Fire和Forget不是一个选项(除非您实现自己的FireAndForget)

#3


0  

Mapad posted a link to on UI and threads here which you may find useful. You didn't mention which UI toolkit and which language so I can't give you any specifics.

Mapad在这里发布了一个关于UI和线程的链接,您可能会觉得这些链接很有用。你没有提到哪个UI工具包和哪种语言,所以我不能给你任何细节。