【发布时间】:2014-08-07 21:40:09
【问题描述】:
这里有一些示例代码可以更好地说明我在这里想要完成的工作。 基本上我需要设置只能从 UI 线程设置的属性。有什么想法吗?
public ref class ExtendedImage : public System::Windows::Controls::Image
{
public:
void SetImageFromUrl (System::String^ url)
{
if (!System::Uri::TryCreate (path, System::UriKind::Absolute, this->m_uri) || this->m_uri->IsFile)
return;
System::Threading::Thread^ downloadImage = gcnew System::Threading::Thread (gcnew System::Threading::ThreadStart (this, &ExtendedImage::DownloadAndSetImage));
downloadImage->Start ();
}
private:
System::Uri^ m_uri;
void DownloadAndSetImage ()
{
System::Windows::Media::Imaging::BitmapImage^ bitmap = gcnew System::Windows::Media::Imaging::BitmapImage (this->m_uri);
//execute this->Source = bitmap; on UI thread
}
}
更新:
结合问题代码以正确答案 C# 解决方案后的一些有用信息。要获取 UI 线程调度程序,请使用 System::Windows::Application::Current->Dispatcher。
【问题讨论】:
标签: wpf multithreading c++-cli ui-thread