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

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

Using the WPF Dispatcher in unit tests

By using the Visual Studio Unit Test Framework you don’t need to initialize the Dispatcher yourself. You are absolutely right, that the Dispatcher doesn’t automatically process its queue. You can write a simple helper method “DispatcherUtil.DoEvents()” which tells the Dispatcher to process its queue. C# Code: public static class DispatcherUtil { [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] … Read more

No WebApplicationContext found: no ContextLoaderListener registered?

You’ll have to have a ContextLoaderListener in your web.xml – It loads your configuration files. <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> You need to understand the difference between Web application context and root application context . In the web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. … Read more

Run code on UI thread in WinRT

It’s easier to directly get the CoreWindow from the non-UI thread. The following code will work everywhere, even when GetForCurrentThread() or Window.Current returns null. CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, <lambda for your code which should run on the UI thread>); for example: CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Your UI update code goes here! }); You’ll need to reference Windows.ApplicationModel.Core … Read more

Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher

My gut tells me that Application.Current.Dispatcher will never change and is global to all threads in the current application, while Dispatcher.CurrentDispatcher may create a new instance of Dispatcher depending on the thread from which it was called. That is correct. Additionally, there is no point whatsoever in accessing Dispatcher.CurrentDispatcher from a non-UI thread. It will … Read more

How to put delay before doing an operation in WPF

The call to Thread.Sleep is blocking the UI thread. You need to wait asynchronously. Method 1: use a DispatcherTimer tbkLabel.Text = “two seconds delay”; var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) }; timer.Start(); timer.Tick += (sender, args) => { timer.Stop(); var page = new Page2(); page.Show(); }; Method 2: use Task.Delay tbkLabel.Text = … Read more