我正在尝试使用这样的异步调用从 Internet 下载图像:
private void DoGetAlbumart(object sender, DoWorkEventArgs e)
{
string req = (string)e.Argument;
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(ReadWebRequestCallback);
wc.OpenReadAsync(new Uri(req));
}
void ReadWebRequestCallback( object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
try
{
BitmapImage image = new BitmapImage();
image.SetSource(e.Result);
SecondTile.Source = image;
}
catch (Exception ex)
{
}
}
else
{
}
}
似乎当断点在 BitmapImage image = new BitmapImage() 处命中时,我得到了以下异常:
ex = {System.UnauthorizedAccessException:无效的跨线程访问。在 MS.Internal.XcpImports.CheckThread() 在 System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO) 在 System.Windows.Media.Imaging.BitmapImage..ctor()
我还能尝试什么来摆脱这个错误?