You need to save Dispatcher.CurrentDispatcher from the main thread:
public partial class MainWindow : Window {
Then, when you need to do something in the context of the main thread, follow these steps:
MainWindow.dispatcher.Invoke(() => { label1.Content = s; });
Note. Dispatcher.BeginInvoke does this asynchronously, unlike Dispatcher.Invoke . You probably need a synchronous call . In this case, the asynchronous call looks fine, but often you may need to update the user interface on the main end, and then continue the current thread, knowing that the update is complete.
Here's a similar question with a complete example.
source share