在创建后台线程WP7的BitmapImage [英] Creating BitmapImage on background thread WP7

查看:152
本文介绍了在创建后台线程WP7的BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个UnauthorizedAccessException(无效的跨线程访问。)在后台(线程池)线程运行以下code时,这是预期的行为?

  VAR URI =新的URI(resourcevault /图像/ defaultSearch.png,UriKind.Relative);
 VAR信息= Application.GetResourceStream(URI); //这一行抛出异常....
 this.defaultSearchImage =新的BitmapImage();


解决方案

究其原因是因为你的后台线程不能直接用于更新UI。相反,你需要使用调度元帅到UI线程的数据。事情是这样的:

  VAR URI =新的URI(resourcevault /图像/ defaultSearch.png,UriKind.Relative);
VAR信息= Application.GetResourceStream(URI);Dispatcher.BeginInvoke(()=> {
    this.defaultSearchImage =新的BitmapImage();
});

I'm receiving an UnauthorizedAccessException ("Invalid cross-thread access.") when running the following code on a background (threadpool) thread, is this expected behaviour?

 var uri = new Uri("resourcevault/images/defaultSearch.png", UriKind.Relative);
 var info = Application.GetResourceStream(uri);

 // this line throws exception....
 this.defaultSearchImage = new BitmapImage();

解决方案

The reason is because your background thread cannot directly be used to update the UI. Instead, you need to use a Dispatcher to marshal the data on to the UI thread. Something like this:

var uri = new Uri("resourcevault/images/defaultSearch.png", UriKind.Relative);
var info = Application.GetResourceStream(uri);

Dispatcher.BeginInvoke(() => {        
    this.defaultSearchImage = new BitmapImage();
});

这篇关于在创建后台线程WP7的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆