Download BitmapImage using Task Parallel Library

Downloading a BitmapImage from a URI on the UI thread hangs up the application. To avoid this, I'm trying to download the BitmapImage in a separate thread, would like to know if it can be made simple with TPL.

My current method is the following:

Task.Factory.StartNew<BitmapImage>(() => new BitmapImage(myUri) { CacheOption = BitmapCacheOption.OnLoad }, CancellationToken.None, TaskCreationOptions.None, new StaTaskScheduler(1)) .ContinueWith(t => image1.Source = t.Result, TaskScheduler.FromCurrentSynchronizationContext());

The problem with this is of course the fact, that the BitmapImage wasn't created on the same thread as the Image.

The BitmapImage loaded from URI cannot be frozen, so that wouldn't work normally.

Currently, the simplest method I can think of is creating a stream from the URI, then loading the bitmap from that stream and freeze it before returning. That should create a really bloating code I believe.

Is there a way to keep the code small(er) and (more) readable while trying to display an image from the web?

-------------Problems Reply------------

If the image is in an XAML file and you're using binding to load it, set IsAsync to true to download it async.

Of course, then it is good practice to provide a fallback place-holder before the image shows up. One way to do it is to use PriorityBinding.

If you must do it programatically, try creating an async ImageSource then setting the BitmapImage's source to it.

Category:wpf Views:0 Time:2011-03-29

Related post

  • Using .NET (3.5) Task Parallel Library in C++/ CLI 2011-01-20

    Well, I download Reactive Extensions for NET 3.5 to use it in visual studio 2008 with c++/cli... But all Task Parallel Library examples are in C#...I can not able to figure out EVEN converting that simple C# statements into C++/ CLI... // use an Acti

  • Should i use ThreadPools or Task Parallel Library for IO-bound operations 2011-03-06

    In one of my projects that's kinda an aggregator, I parse feeds, podcasts and so from the web. If I use sequential approach, given that a large number of resources, it takes quite a time to process all of them (because of network issues and similar s

  • Events with Task Parallel Library 2011-10-20

    I'm using the .NET 4.0's Task Parallel Library for executing a long running task. The long running task has different stages and at each stage it completes I want to log some information to a text file or db. Instead of calling the log methods at eve

  • Support of progress reporting and incremental results in .NET 4.0 "Task Parallel Library" 2009-10-08

    I know that Task Parallel Library is still in Beta and there are likely to be less resources available but from whatever I have read, library gives very special treatment to task scheduling, exception handling and cancellation. But I don't find any r

  • Task Parallel Library vs Async Workflows 2009-12-09

    I have some stuff written in c# that executes concurrent code, making heavy use of the Task Parallel Library (Task and Future continuation chains). I'm now porting this to F# and am trying to figure out the pros and cons of using F# Async workflows v

  • Exception Handling (Task Parallel Library) .Net Framework 4.0 Beta 2 2010-01-11

    at the moment I am trying some of the new Features of the Task Parallel Library, shipped with the .Net Framework 4.0 Beta 2. My question relates specifically to the Exception Handling within the TPL as described here:

  • Is it too early to start designing for Task Parallel Library? 2010-01-28

    I have been following the development of the .NET Task Parallel Library (TPL) with great interest since Microsoft first announced it. There is no doubt in my mind that we will eventually take advantage of TPL. What I am questioning is whether it make

  • How to handle all unhandled exceptions when using Task Parallel Library? 2010-04-25

    I'm using the TPL (Task Parallel Library) in .NET 4.0. I want to be able to centralize the handling logic of all unhandled exceptions by using the Thread.GetDomain().UnhandledException event. However, in my application, the event is never fired for t

  • .net 4.0 Task Parallel Library vs. MPI.NET 2010-06-03

    Does .net 4.0 Task Parallel Library replace MPI.NET for High-performace computings? MPI.NET found here is a high-performance, easy-to-use implementation of the Message Passing Interface (MPI) for Microsoft'

  • Can I use the task parallel library in a .Net 3.5 project? 2010-06-07

    I heard that the Task Parallel Library can be used in a .Net 3.5 project. Is this correct, and if yes, how do I use it? In .Net 4.0, it resides in System.Threading, but when I select .Net 3.5 as the target in Visual Studio 2010, I don't get access to

  • Task parallel library replacement for BackgroundWorker? 2010-08-18

    Does the task parallel library have anything that would be considered a replacement or improvement over the BackgroundWorker class? I have a WinForms application with a wizard-style UI, and it does some long-running tasks. I want to be able to have a

  • Is there an interface wrapper around the Task Parallel Library so that I can swap it out for unit testing? 2010-08-23

    I asked this question a while ago. I now know it is a Bad Idea and that encapsulating the scheduling and running of tasks should be abstracted so that one can pass in a synchronous scheduler from the unit tests. I've currently got code that uses the

  • How do I chain Asynchronous Operations with the Task Parallel library in .NET 4? 2010-09-07

    I'm attempting to programmatically chain asynchronous operations in C#4, such as Writes to a given Stream object. I originally did this "manually", hooking callbacks from one operation to the next, but I thought I'd try the .NET 4 Task Parallel Libra

  • Is there an alternative to CHESS for testing programs written with Task Parallel Library 2010-10-03

    I am looking for a tool that will allow me to execute unit tests over all possible interleavings (spelling?) with a program that was written with Task Parallel Library constructs. Alternatively, how could I run CHESS with dotNET 4.0? --------------So

  • Using Task Parallel Library with Multiple Computers 2010-10-05

    Is there any way to use Task Parallel Library in multi computer scenarios ? I mean if i have huge number of tasks , can i schedule it over LAN in number of servers ? --------------Solutions------------- The TPL is geared towards single computer, mult

  • Task Parallel library vs Native Thread Synchronization 2010-10-21

    This question may be subjective but i just want to know when to use TPL and when to use native thread based synchronization (evnts, wait handles). Will the native thread synchronization techniques be obsolete going forward? Thanks --------------Solut

  • Will Reactive Extensions (Rx) supercede the Task Parallel Library? 2010-11-05

    After following through the samples of Rx.NET, I'm gob smacked at how brilliant the concept and implementation of Reactive Extensions are. It appears to offer developers a more maintainable pattern for achiving the same sort of multi-threaded paralle

  • How to Schedule a task for future execution in Task Parallel Library 2010-11-19

    Is there a way to schedule a Task for execution in the future using the Task Parallel Library? I realize I could do this with pre-.NET4 methods such as System.Threading.Timer ... however if there is a TPL way to do this I'd rather stay within the des

  • How do Tasks in the Task Parallel Library affect ActivityID? 2010-12-02

    Before using the Task Parallel Library, I have often used CorrelationManager.ActivityId to keep track of tracing/error reporting with multiple threads. ActivityId is stored in Thread Local Storage, so each thread get's its own copy. The idea is that

Copyright (C), All Rights Reserved.

processed in 0.282 (s). 11 q(s)