2010-04-09 83 views
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會阻止當前的UI線程,所以ImageOpened/ImageFailed事件永遠不會被觸發。你能否建議我一個簡單的解決方法來等待多個ui事件?使用ManualResetEvent等待多個Image.ImageOpened事件

回答

1

爲什麼不在另一個線程上執行WaitAll,然後在完成時使用Dispatcher將調用返回給UI線程?