WPF 小小案列(同步异步)
Posted loushengjie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 小小案列(同步异步)相关的知识,希望对你有一定的参考价值。
private void BtnButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("hello Word");
}
private void ModifyUI()
{
// 模拟一些工作正在进行
Thread.Sleep(TimeSpan.FromSeconds(2));
//lblHello.Content = "欢迎你光临WPF的世界,Dispatcher";
this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
{
lblHello.Content = "欢迎你光临WPF的世界,Dispatche 同步方法 !!"+DateTime.Now.ToString();
MessageBox.Show("你好我是多线程同步方法");
});
}
private void btnThd_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(ModifyUI);
thread.Start();
}
/// <summary>
/// 异步方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAppBeginInvoke_Click(object sender, RoutedEventArgs e)
{
new Thread(() =>
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(2));
this.lblHello.Content = "欢迎你光临WPF的世界,Dispatche 异步方法!!" + DateTime.Now.ToString();
MessageBox.Show("你好我是多线程异步方法");
}));
}).Start();
}
以上是关于WPF 小小案列(同步异步)的主要内容,如果未能解决你的问题,请参考以下文章