2014-02-26 46 views
0

我想調用兩個或更多個方法。當一個函數執行get完成時,我需要調用另一個方法。基本上我試圖在我的應用程序中實現備份功能。我正在開發Windows Phone應用程序,它需要備份聯繫人,圖像,視頻。我已經爲每個這些創建了方法。當聯繫人備份完成後,我想調用圖像方法。我爲這些創建了不同的功能。我怎樣才能一個接一個地調用這些函數?一個接一個地調用兩個或多個函數

我試過這樣的事情。

// Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 

     UploadImagesThread = new Thread(UploadImages); 
     UploadContactsThread = new Thread(UploadContacts); 
     // Sample code to localize the ApplicationBar 
     //BuildLocalizedApplicationBar(); 
    } 

在上傳按鈕點擊

if (chkContacts.IsChecked.Value) 
{ 
    Dispatcher.BeginInvoke(() => 
    { 
     SystemTray.ProgressIndicator.Text = "Searching contacts..."; 
    }); 
    UploadContactsThread.Start(); 
} 
if (chkImages.IsChecked.Value) 
{ 
    Dispatcher.BeginInvoke(() => 
    { 
      SystemTray.ProgressIndicator.Text = "Compressing images..."; 
    }); 
    UploadImagesThread.Start(); 
} 

,但它不會幫助我。我如何充實?我UploadContact方法異步方法調用這樣

Contacts objContacts = new Contacts(); 
objContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(objContacts_SearchCompleted); 
objContacts.SearchAsync(string.Empty, FilterKind.None, null); 

回答

2

嘗試使用任務,這將讓你給你一個線程池是免費的。你不能這樣做在一個構造函數,但你可以重寫你的OnNavigatedTo Mehtod:

protected override async void OnNavigatedTo(NavigationEventArgs e) 
    { 

     base.OnNavigatedTo(e); 

     await Task.Run(() => { UploadImages(); }); 
     await Task.Run(() => { UploadContacts(); }); 
    } 

的AWAIT將確保您的圖像完成後,觸點會開始上傳。您還可以查看background uploading這意味着您的應用在完成操作時無需運行。

+0

它給了我一個錯誤'的調用以下方法或屬性之間曖昧:「System.Threading.Tasks.Task.Run(系統.Func )'和'System.Threading.Tasks.Task.Run(System。行動)'' –

+0

@AjayPunekar對不起,我更新了我的怒氣。我在猜測UploadImages得到了帶返回類型任務的簽名? – Mark

+0

這兩種方法都有返回類型'void' –

1

您的問題(調用一個接一個的方法?)的描述不是很清楚,但看着你的代碼,我想您在開始UploadContactsThread之前等待UploadImagesThread線程完成。

使用任務和異步/等待關鍵字是這樣的:

private async void OnButtonClick(object sender, ...) 
{ 
    if (chkContacts.IsChecked.Value) 
    { 
     SystemTray.ProgressIndicator.Text = "Searching contacts..."; 
     await Task.Run(() => UploadImages()); 
    } 
    if (chkImages.IsChecked.Value) 
    { 
     SystemTray.ProgressIndicator.Text = "Compressing images..."; 
     await Task.Run(() => UploadContacts()); 
    } 
} 

注:假設你的代碼2號地塊在UI線程上運行,你不應該需要使用BeginInvoke


編輯

針對最近的更改:你需要重新設計。試試這個:

private async void OnButtonClick(object sender, ...) 
{ 
    bool uploadContacts = chkContacts.IsChecked.Value; 
    bool uploadImages = chkImages.IsChecked.Value; 

    //use this if the continuation runs on the UI thread 
    Action continuation = async() => { 
    if(uploadImages) { 
     SystemTray.ProgressIndicator.Text = "Compressing images..."; 
     await Task.Run(() => UploadImages()); 
    } 
    }; 

    //OR this if it doesn't 
    Action continuation =() => { 
    if(uploadImages) { 
     Dispatcher.BeginInvoke(() => SystemTray.ProgressIndicator.Text = "Compressing images..."); 
     UploadImages(); 
    } 
    }; 

    if (uploadContacts) 
    { 
     SystemTray.ProgressIndicator.Text = "Searching contacts..."; 
     UploadContacts(continuation); 
    } 
} 

private void UploadContacts(Action continuation) 
{ 
    Contacts objContacts = new Contacts(); 

    //when the search has finished, trigger your event handler AND the continuation task, which will upload the images 
    objContacts.SearchCompleted += objContacts_SearchCompleted; 
    objContacts.SearchCompleted += (sender, args) => continuation(); 

    objContacts.SearchAsync(string.Empty, FilterKind.None, null); 
} 
+0

它給了我一個錯誤'調用之間的模糊以下方法或屬性:'System.Threading.Tasks.Task。運行(System.Func )'和'System.Threading.Tasks.Task.Run(System.Action)'' –

+0

@AjayPunekar修復,更新了我的答案。 – dcastro

+0

它給了我錯誤'只有賦值,調用,遞增,遞減,等待和新對象表達式可以用作語句'。函數返回類型是void。 –

0

嘗試類似的東西:

 bool backContact = chkContacts.IsChecked.Value; 
     bool backImages = chkImages.IsChecked.Value; 
     Task.Factory.StartNew(() => 
        { 
         if (backContact) { 
          Dispatcher.BeginInvoke(() => 
          { 
           SystemTray.ProgressIndicator.Text = "Searching contacts..."; 
          }); 
          UploadContacts; 
         }); 
        }).ContinueWith(() => 
         { 
          if (backImages) { 
           Dispatcher.BeginInvoke(() => 
          { 
           SystemTray.ProgressIndicator.Text = "Compressing images..."; 
          }); 
          UploadImages; 
          } 
         } 
相關問題