0

In .NET 4.x I could run code in a non-UI thread and update a WPF (or UWP) control in the UI thread with something like this (Messages is a string property in the ViewModel referenced by the UI which triggers OnPropertyChanged()):

App.Current.Dispatcher.Invoke(new Action(() => Messages += (message + Environment.NewLine)));

How can you access the UI thread with .NET 6?

I've looked all thru the System.Threading namespace and can't find any reference to a dispatcher similar that found in .NET 4.x. I can find a reference to the current thread thru System.Threading.Thread.CurrentThread, but I'm not sure how to dispatch an action into that thread.

Russ_S
  • 1
  • 1
  • Dispatcher is not a language feature of C#, it is part of the particular framework. It works the same in WPF on .NET 6 as it did in .NET Framework. – Crowcoder Dec 12 '22 at 20:45
  • https://stackoverflow.com/questions/41748335/net-dispatcher-for-net-core – Hans Passant Dec 12 '22 at 21:15
  • .NET 6 is not .NET Core. I wouldn't use that question as reference here. – Crowcoder Dec 12 '22 at 21:23
  • .NET5 and up are .NETCore editions, running on CoreCLR. Microsoft renamed them to ".NET" to emphasize that is the future direction with .net – Hans Passant Dec 12 '22 at 21:59
  • @HansPassant what I mean is that WPF has advanced since 3.1. There are no hoops to jump through to access Dispatcher. – Crowcoder Dec 13 '22 at 15:33
  • Sorry, should have specified WinUI Desktop, I added the appropriate tag. Thanks everyone for your help. – Russ_S Dec 16 '22 at 16:06

1 Answers1

-1

Since you know how to use the Dispatcher in WPF and UWP, I guess your question is about WinUI 3. For WinUI 3, use DispatcherQueue. Bisedes that, there is DispatcherHelper from Community Toolkit.

Oleg Mikhailov
  • 252
  • 1
  • 8