欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF 小小案列(同步异步)

程序员文章站 2022-03-04 13:26:15
private void BtnButton_Click(object sender, RoutedEventArgs e) { MessageBox.Show("hello Word"); } private void ModifyUI() { // 模拟一些工作正在进行 Thread.Sleep ......

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

WPF 小小案列(同步异步)