views:

30

answers:

1
Dictionary<Image, ManualResetEvent> waitHandleMap = new Dictionary<Image, ManualResetEvent>();
List<Image> images = GetImagesWhichAreAlreadyInVisualTree();
foreach (var image in images)
{
    image.Source = new BitmapImage(new Uri("some_valid_image_url"));
    waitHandleMap.Add(image, new ManualResetEvent(false));
    image.ImageOpened += delegate { waitHandleMap[image].Set(); };
    image.ImageFailed += delegate { waitHandleMap[image].Set(); };
}            
WaitHandle.WaitAll(waitHandleMap.Values.ToArray());

WaitHandle.WaitAll blocks the current UI thread, so ImageOpened/ImageFailed events would never get fired. Could you suggest me an easy workaround to wait for the multiple ui events?

+1  A: 

Why not perform the WaitAll on another thread, and then use the Dispatcher to marshal a call back to the UI thread when that's finished?

Jon Skeet