新建sl4 应用程序,在mainpage下添加代码:
<button x:name="btnthread1" click="btnthread1_click">thread1</button>
后台代码为:
这个问题的原因很简单:一个线程尝试调用另一个线程的方法 解决这个异常的方式很简单,
1:使用dependencyobject.dispatcher.begininvoke 方法:
new thread(() =>
{
context.post((state) =>
{
messagebox.show("hello world");
}, null);
}).start();
}
new thread(() =>
{
context.post((state) =>
{
messagebox.show("hello world");
}, null);
}).start();
}).start();
}
所以提出了第三种方案:
1:新建静态类uisynchronizationcontext,代码如下:
public app()
{
this.startup += this.application_startup;
this.exit += this.application_exit;
this.unhandledexception += this.application_unhandledexception;
//保存ui线程同步上小文
uisynchronizationcontext.context = synchronizationcontext.current;
initializecomponent();
}
system.windows.deployment.current.dispatcher.begininvoke(() =>
{
messagebox.show("hello world");
});
}).start();
}).start();
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论