WPF 小小案列(同步异步)
2021-06-08 02:03
标签:obj src com show rgs object tostring mes modify private void BtnButton_Click(object sender, RoutedEventArgs e) private void btnThd_Click(object sender, RoutedEventArgs e) this.lblHello.Content = "欢迎你光临WPF的世界,Dispatche 异步方法!!" + DateTime.Now.ToString(); WPF 小小案列(同步异步) 标签:obj src com show rgs object tostring mes modify 原文地址:https://www.cnblogs.com/loushengjie/p/10711846.html
{
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("你好我是多线程同步方法");
});
}
{
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));
MessageBox.Show("你好我是多线程异步方法");
}));
}).Start();
}