Consider the code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
//System.Threading.Thread.CurrentThread = button.Dispatcher.Thread
button.Dispatcher.Invoke(() => button.Content = "1234");
}
}
Of course, button_Click
is run on the main thread.
My understanding is that button.Dispatcher.Thread
is the main thread and the Invoke()
will get processed only when the thread is not blocked. However, isn't the main thread blocked in this case? I.e. the main thread is waiting for the Dispatcher.Invoke()
call to complete and Dispatcher.Invoke()
is waiting for the main thread to free up. So I expect a deadlock here, but it doesn't get deadlocked.
Why?
P.S: I am aware that in this situation I don't need the Dispatcher.Invoke
and that I can call the button.Content = "1234"
directly. I am trying to understand why the deadlock DOES NOT happen in this case.