c# – WPF Dispatcher {“调用线程不能访问此对象,因为不同的线
发布时间:2020-12-15 06:36:40 所属栏目:百科 来源:网络整理
导读:首先我需要说的是我和 WPF和C#的noob. 应用:创建Mandelbrot图像(GUI) 我的调度员完全可以这样做: private void progressBarRefresh(){ while ((con.Progress) 99) { progressBar1.Dispatcher.Invoke(DispatcherPriority.Send,new Action(delegate { progre
首先我需要说的是我和
WPF和C#的noob.
应用:创建Mandelbrot图像(GUI) 我的调度员完全可以这样做: private void progressBarRefresh(){ while ((con.Progress) < 99) { progressBar1.Dispatcher.Invoke(DispatcherPriority.Send,new Action(delegate { progressBar1.Value = con.Progress; } )); } } 当使用以下代码执行此操作时,我收到消息(标题): bmp = BitmapSource.Create(width,height,96,pf,null,rawImage,stride); this.Dispatcher.Invoke(DispatcherPriority.Send,new Action(delegate { img.Source = bmp; ViewBox.Child = img; //vllt am schluss } )); 我将尝试解释我的程序如何工作. 当我不把计算在一个单独的线程,那么我可以刷新或构建我的图像. 解决方法
为了防止在不同线程之间共享对象,请始终在UI线程上创建该对象.稍后当您想要访问对象时,可以检查是否可以访问对象.如果您没有访问权限,请重新使用UI线程访问功能.示例代码如下:
private void YourMethod() { if (Application.Current.Dispatcher.CheckAccess()) { // do whatever you want to do with shared object. } else { //Other wise re-invoke the method with UI thread access Application.Current.Dispatcher.Invoke(new System.Action(() => YourMethod())); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |