如何等到UploadStringAsync方法完成

时间:2015-10-14 04:39:59

标签: c# windows-phone-8 async-await

我正在尝试/想要等到异步任务完成,这就是我想要做的事情

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");

那么如何实现呢?我试图让方法异步,然后等待它,但这没有帮助

4 个答案:

答案 0 :(得分:3)

使用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);

    }

答案 1 :(得分: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;
}

答案 2 :(得分:1)

你可以使用回调来做到这一点。如何定义回调由您决定。作为一个例子,我将它定义为一个带有两个参数的Action:一个可用于向调用者返回内容的对象,以及在保存期间出现错误时的异常。

1367493814.0  2/5/2013 12:23:34
1357129414.0  Monday 2 Jan 2013 12:23:34
1357129414.0  mon 2 Jan 2013 12:23:34
1444817460.0  10/14/2015 11:11
0             10 2015

并从其他页面调用该方法

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)
    }
}

答案 3 :(得分: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");