2011-03-26 47 views
0

這是一個Windows Phone7項目。 我在我的代碼中有幾個異步調用,最後,填充一個全局的List。但是我的問題是,我怎麼知道的異步作業完成了,這樣我就可以在代碼中繼續了? 編輯我更新的代碼:如何知道我的異步功能何時完成? (windows phone7 dev)

private void GetFlickrPhotos(Action finishedCallback) 
    { 
     Action<FlickrResult<PhotoCollection>> getPhotoCollectionCallback; 
     getPhotoCollectionCallback = GetPhotoCollection; 
     flickr.InterestingnessGetListAsync(getPhotoCollectionCallback); 
     finishedCallback(); 
    } 

    private void GetPhotoCollection(FlickrResult<PhotoCollection> photoColl) 
    { 
     PhotoCollection photoCollection = (PhotoCollection)photoColl.Result; 

     foreach (Photo photo in photoCollection) 
     { 
      flickrPhotoUrls.Add(photo.MediumUrl); 
     }   
    } 

    private void Grid_Loaded(object sender, RoutedEventArgs e) 
    { 
     GetFlickrPhotos(() => 
     { 
      int test = flickrPhotoUrls.Count; 
     });    
    } 

異步調用.NET框架4.使用Action<T>它仍然不會等待異步調用完成。是因爲異步調用是從「GetFlickrPhotos」完成的嗎?

回答

1

我去了下面的代碼,它爲我的程序工作。感謝您的幫助!

app.Flickr.PhotosSearchAsync(options, flickrResult => 
      { 
       if (flickrResult.HasError) 
       {           
        ShowErrorImage(); 
       } 
       else 
       {       
        flickrPhotoUrls.Clear(); 
        flickrImages.Clear(); 
        foreach (var item in flickrResult.Result) 
        { 
         FlickrImage image = new FlickrImage(); 
         image.ImageOwner = item.OwnerName; 
         image.DateTaken = item.DateTaken; 
         if (item.Title == string.Empty) 
         { 
          image.ImageTitle = "Untitled"; 
         } 
         else 
         { 
          image.ImageTitle = item.Title; 
         } 
         image.SmallUrl = item.SmallUrl; 
         image.MediumUrl = item.MediumUrl; 
         image.Description = item.Description; 
         image.Latitude = item.Latitude; 
         image.Longitude = item.Longitude; 
         if (item.DoesLargeExist == true) 
         { 
          image.LargeUrl = item.LargeUrl; 
         } 

         flickrImages.Add(image); 
        } 
        ShowPhotos(); 
       }      
      }); 

這樣,我調用該方法ShowPhotos()當Flickr的通話結束。

0

調查IAsyncResult - 這裏有很多與這個特定模式相關的資源,但基本上這將允許您維護一個對象的實例,它的作用就是讓您確定操作的當前狀態 - 甚至專門撥打End方法來中斷操作。

你的方法簽名可能會是這個樣子,比如:

public IAsyncResult BeginOperation(AsyncCallback callback) 

public EndOperation(IAsyncResult result) 

MSDN

IAsyncResult接口是 含有 方法,可以異步操作 類實現的。它是該啓動 異步操作的方法,如 IsolatedStorageFileStream.BeginRead, 返回類型 並傳遞給結束 異步操作的方法,如 IsolatedStorageFileStream.EndRead。一個 IAsyncResult也傳遞給 方法,當 異步操作完成時,由 AsyncCallback委託調用該方法。

支持用於異步 操作 IAsyncResult接口存儲狀態 信息,並且提供了 同步對象,以允許 操作完成時發出信號 線程的對象。

編輯:

行,除非我失去了一些東西,然後一個簡單的事件通知可能是所有你所需要的 - 考慮一類下面介紹以下用法:

var flickrOperation = new FlickrOperation(); 
flickrOperation.FlickrPhotoURLsLoaded += 
    delegate(object sender, EventArgs e) 
    { 
     var photoCount = flickrOperation.FlickrPhotoURLs.Count; 
    }; 
flickrOperation.BeginGetFlickrPhotoURLs(); 

現在讓我們定義這個類,雖然是原始的,只是在這種情況下達到目的的一種手段。請注意,特別是聲明的使用FlickrPhotoURLsLoaded - 這是它在操作完成燃煤(如回調加載的URL完成規定)事件:

class FlickrOperation 
{ 
    //the result: 
    //ultimately, hide this and expose a read only collection or something 
    public List<string> FlickrPhotoURLs = new List<string>(); 
    //the event: 
    //occurs when all returns photo URLs have been loaded 
    public event EventHandler FlickrPhotoURLsLoaded; 

    public void BeginGetFlickrPhotoURLs() 
    { 
     //perform the flickr call... 
     var getPhotoCollectionCallback = GetFlickrPhotoURLsCallback; 
     flickr.InterestingnessGetListAsync(getPhotoCollectionCallback); 
    } 

    private void GetFlickrPhotoURLsCallback(FlickrResult<PhotoCollection> result) 
    { 
     //perform the url collection from flickr result... 
     FlickrPhotoURLs.Clear(); 
     var photoCollection = (PhotoCollection)result.Result; 
     foreach (Photo photo in photoCollection) 
     { 
      flickrPhotoUrls.Add(photo.MediumUrl); 
     } 
     //check to see if event has any subscribers... 
     if (FlickrPhotoURLsLoaded != null) 
     { 
      //invoke any handlers delegated... 
      FlickrPhotoURLsLoaded(this, new EventArgs()); 
     } 
    } 
} 
+0

這可能與我發佈的更新代碼有關嗎? – 2011-03-26 11:54:43

+0

謝謝,但仍然,我得到我的URL列表填充的唯一方法是通過在調用代碼上設置斷點:FlickrPhotoURLsLoaded(this,new EventArgs());如果我不在這裏設置斷點,代碼只是進入下一行。是否有可能使代碼等待調用完成,然後繼續? – 2011-03-26 18:05:17

0

在最簡單的情況下,你可以提供一個回調,或者多花些功夫,創建一個在工作完成時引發的事件。這裏是一個回調代碼:

private void Grid_Loaded(object sender, RoutedEventArgs e) 
{ 
    DoAsyncWork(()=>{ 
     //continue here 
    }); 

} 

private void DoAsyncWork(Action finishedCallback) 
{ 
    //Doing some async work, and in the end of the last async call, 
    //a global List<string> myList is filled 
    //at this point call: 
    //finishedCallback(); 
} 
+0

我更新了代碼,但正如您所看到的,異步調用是在下一個函數中完成的。我如何用我更新的代碼來做到這一點?我完全陌生,這:) – 2011-03-26 11:53:46

相關問題