Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a value with Dispatcher.Invoke?

Anyone knows how to return a value from Dispatcher.Invoke in wpf? I want to return the selected index for a ComboBox.

Thanks!

like image 491
toni Avatar asked Mar 22 '10 11:03

toni


People also ask

What does dispatcher invoke do?

Invoke(Action, DispatcherPriority, CancellationToken)Executes the specified Action synchronously at the specified priority on the thread the Dispatcher is associated with.

What is DispatcherPriority?

Describes the priorities at which operations can be invoked by way of the Dispatcher.

When to use Dispatcher in WPF?

A dispatcher is often used to invoke calls on another thread. An example would be if you have a background thread working, and you need to update the UI thread, you would need a dispatcher to do it.


1 Answers

There's another way that returns value from Invoke():

object oIsLoaded = container.Dispatcher.Invoke( new Func<bool> ( () =>     {         return container.IsLoaded;     }) ); 

And by the way, chances are that the initial code (which is working with delegate) won't modify oIsLoaded at all; So I'd rather use a Func<> for returning a value from that kind of function.

like image 131
user216652 Avatar answered Sep 27 '22 22:09

user216652