WPF 小小案列(同步异步)

2021-06-08 02:03

阅读:469

标签:obj   src   com   show   rgs   object   tostring   mes   modify   

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();
}
///


/// 异步方法
///

///
///
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 小小案列(同步异步)

标签:obj   src   com   show   rgs   object   tostring   mes   modify   

原文地址:https://www.cnblogs.com/loushengjie/p/10711846.html


评论


亲,登录后才可以留言!