WPF BackgroundWorker vs. Dispatcher

The main difference between the Dispatcher and other threading methods is that the Dispatcher is not actually multi-threaded. The Dispatcher governs the controls, which need a single thread to function properly; the BeginInvoke method of the Dispatcher queues events for later execution (depending on priority etc.), but still on the same thread. BackgroundWorker on the … Read more

Dispatcher Invoke(…) vs BeginInvoke(…) confusion

When you use Dispatcher.BeginInvoke it means that it schedules the given action for execution in the UI thread at a later point in time, and then returns control to allow the current thread to continue executing. Invoke blocks the caller until the scheduled action finishes. When you use BeginInvoke your loop is going to run … Read more

Correct way to get the CoreDispatcher in a Windows Store app

This is the preferred way: Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Your UI update code goes here! }); The advantage this has is that it gets the main CoreApplicationView and so is always available. More details here. There are two alternatives which you could use. First alternative Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher This gets the active view for the app, … Read more

The calling thread cannot access this object because a different thread owns it.WPF [duplicate]

Use Dispatcher.Invoke Method. Executes the specified delegate synchronously on the thread the Dispatcher is associated with. Also In WPF, only the thread that created a DispatcherObject may access that object. For example, a background thread that is spun off from the main UI thread cannot update the contents of a Button that was created on … Read more

Cannot convert lambda expression to type ‘System.Delegate’

The problem is that you aren’t providing the exact type of delegate you want to invoke. Dispatcher.Invoke just takes a Delegate. Is it an Action<T>? If so, what is T? Is it a MethodInvoker? Action? What? If your delegate takes no arguments and returns nothing, you can use Action or MethodInvoker. Try this: _uiDispatcher.Invoke(new Action(() … Read more

How to pass the UI Dispatcher to the ViewModel

I have abstracted the Dispatcher using an interface IContext: public interface IContext { bool IsSynchronized { get; } void Invoke(Action action); void BeginInvoke(Action action); } This has the advantage that you can unit-test your ViewModels more easily. I inject the interface into my ViewModels using the MEF (Managed Extensibility Framework). Another possibility would be a … Read more