The Invoke will block current thread and main thread.
And BeginInvoke only block main thread.
you can try in wpf
MainWindow.xaml
<Window x:Class="WpfApp5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<UniformGrid Columns="1">
<TextBlock Text="{Binding TimeString}"/>
<Button Content="Invoke" Click="Invoke_Button_Click"/>
<Button Content="BeginInvoke" Click="BeginInvoke_Button_Click"/>
</UniformGrid>
</Window>
MainWindow.xaml.cs
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace WpfApp5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string tmeString;
public string TimeString
{
get { return this.tmeString; }
set
{
this.tmeString = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TimeString)));
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
Task.Run(() =>
{
while (true)
{
TimeString = $"DateTimeNow : {DateTime.Now}";
Thread.Sleep(1000);
}
});
}
private void BeginInvoke_Button_Click(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke((Action)SomeWork, null);
//break point here
bool buttonClickEventEnd = true;
}
private void Invoke_Button_Click(object sender, RoutedEventArgs e)
{
Dispatcher.Invoke((Action)SomeWork, null);
//break point here
bool buttonClickEventEnd = true;
}
private void SomeWork()
{
Thread.Sleep(3 * 1000);
}
}
}