2015-10-14 43 views
0

我想/想等到異步任務在這裏完成的是什麼我試圖做如何等待,直到UploadStringAsync方法完成

public class savetodbsingle 
{ 
    public static string insert(string id, string type, string cat) 
    { 
     Uri uri = new Uri("http://yxz.com"); 

     WebClient wc = new WebClient(); 
     wc.Headers[HttpRequestHeader.ContentType] = "application/json"; 
     wc.UploadStringAsync(uri, "POST", json); 

     wc.UploadStringCompleted += wc_UploadComplete6; 
    } 

    private static void wc_UploadComplete6(object sender, UploadStringCompletedEventArgs e) 
    { 
     //Saving to database logic here 
    } 
} 

,並從其他網頁

//calling method 
savetodbsingle.insert(id,type,cat)); 

// i want to show messegebox from here when above task completes 
MessegeBox.Show("Completed"); 

調用方法那麼如何實現這一目標呢?我曾試圖讓異步方法,然後等待,但沒有幫助

+0

解決方案你有機會獲得['UploadStringTaskAsync'](HTTPS ://msdn.microsoft.com/en-us/library/hh138531(v = vs.110).aspx)在Windows Phone?如果沒有,你有權訪問類['TaskCompletionSource'](https://msdn.microsoft.com/en-us/library/dd449174(v = vs.110).aspx)? –

+0

不,我沒有訪問它,我不知道TaskCompletionsource @ScottChamberlain – SD7

+0

它給錯誤無法等待void @Eldho – SD7

回答

1

使用替代UploadStringTaskAsync方法

這將返回您可以等待任務。

https://msdn.microsoft.com/en-us/library/hh159423%28v=vs.110%29.aspx

public static async string insert(string id, string type, string cat, Action<object,Exception> callback) 
    { 
     ---- 
     string result = await wc.UploadStringTaskAsync(uri, "POST", json); 

    } 
+0

你的代碼並不完全是解決方案,但我已經使用UploadStringTaskAsync作爲所述並且實現了相同的 – SD7

+0

糟糕,你必須將方法標記爲異步,我的不好。 – Igoy

+0

檢查我的答案@Igoy – SD7

2

我不知道是否WebClient.UploadStringTaskAsync帶有Microsoft.Bcl.Async爲Windows Phone 8的,但如果有人想知道它是如何實現的,以及如何使用TaskCompletionSource ,這裏是它的實現,取自the source

[HostProtection(ExternalThreading = true)] 
[ComVisible(false)] 
public Task<string> UploadStringTaskAsync(Uri address, string method, string data) 
{ 
    // Create the task to be returned 
    var tcs = new TaskCompletionSource<string>(address); 

    // Setup the callback event handler 
    UploadStringCompletedEventHandler handler = null; 
    handler = (sender, e) => HandleCompletion(tcs, e, (args) => args.Result, handler, (webClient, completion) => webClient.UploadStringCompleted -= completion); 
    this.UploadStringCompleted += handler; 

    // Start the async operation. 
    try { this.UploadStringAsync(address, method, data, tcs); } 
    catch 
    { 
     this.UploadStringCompleted -= handler; 
     throw; 
    } 

    // Return the task that represents the async operation 
    return tcs.Task; 
} 
1

你可以使用回調來實現。您如何定義回調取決於您。作爲一個例子,我將它定義爲一個Action,它帶有兩個參數:一個對象,可以用來給調用者一些東西,還有一個異常以防萬一在保存期間出錯。

public class savetodbsingle 
{ 
    private Action<object, Exception> _callback; 

    public static string insert(string id, string type, string cat, Action<object,Exception> callback) 
    { 
     Uri uri = new Uri("http://yxz.com"); 

     WebClient wc = new WebClient(); 
     wc.Headers[HttpRequestHeader.ContentType] = "application/json"; 
     wc.UploadStringCompleted += wc_UploadComplete6; 

     _callback = callback; 

     wc.UploadStringAsync(uri, "POST", json); 

    } 

    private static void wc_UploadComplete6(object sender, UploadStringCompletedEventArgs e) 
    { 
     //Saving to database logic here 

     // When done, you can return something if needed 
     // or return an exception when something bad happened. 
     _callback(someObject, someException) 
    } 
} 

,並呼籲其他頁面的方法

//calling method 
savetodbsingle.insert(id,type,cat, handleSaved)); 

private void handleSaved(object o, Exception e) 
{ 
    MessegeBox.Show("Completed"); 
} 
1

這是我如何實現它

public static async Task insert(string id, string type, string cat) 
      { 
     WebClient wc = new WebClient(); 
       wc.Headers[HttpRequestHeader.ContentType] = "application/json"; 


      var responce=  await wc.UploadStringTaskAsync(uri, "POST", json); 
      jsons = responce.ToString().Trim(); 

      saving();//save to database logic here 
    } 

並調用方法這樣

await savetodbsingle.insert(id, type, cat); 
Messegebox.Show("Completed");