views:

768

answers:

3

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.

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?

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.

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

A: 

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.

TheObserver
what i don't understand is how not to block when you call for the update
CiNN
windows_notify is the answer. you need to notify the "parent" widget.
TheObserver
+2  A: 

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

For instance:

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

Jon Skeet
+2  A: 

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)

Bogdan Maxim
The Windows Forms team have previously said that you can just fire and forget for calls to Control.BeginInvoke. Obviously this promise doesn't apply to WPF.
Jon Skeet
They said so, but they said that there isn't any warranty that this will be the same after new service packs or in newer versions
Bogdan Maxim