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