最近在自学wpf多线程编程,经过2天的秃头后,终于理清了即使我开再多的线程,只要我调用BeginInvoke或者invoke,始终会回到主线程中运行,导致界面卡顿,那么基于下面的demo,我有一个疑问
这是xaml代码
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="WindowThd" Height="150" Width="400">
<Grid>
<StackPanel>
<Label x:Name="lblHello" Height="25" Content="{Binding Path=MyStr}"/>
<Button Name="btnThd" Click="btnThd_Click" >Test</Button>
</StackPanel>
</Grid>
</Window>
这是后端代码
public partial class MainWindow : Window
{
Para myPara = new Para();
public MainWindow()
{
this.DataContext = myPara;
InitializeComponent();
}
private void ModifyUI()
{
for (int i = 0; i < 5; i++)
{
myPara.MyStr = "Test" + DateTime.Now.ToString();
Thread.Sleep(1000);
}
//this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
//{
// Thread.Sleep(1000);
// lblHello.Content = "Test" + DateTime.Now.ToString();
//});
}
private void btnThd_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(ModifyUI);
thread.IsBackground = false;
thread.Start();
}
}
public class Para : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private string myStr;
public string MyStr
{
get { return myStr; }
set { myStr = value;OnPropertyChanged(nameof(MyStr));}
}
}
测试功能就是点击按钮后新建一个线程,调用一个函数每过一段时间刷新一个字符串显示
这个程序本身没啥意义,主要是我在开始操作不当把sleep放到invoke里了导致卡顿,后来我通过inotifypropertychanged绑定后就没用invoke了,所以我有个疑问,如果直接通过bangding,那invoke有什么使用的意义呢?