如何等待 UploadStringAsync 方法完成
how to await till UploadStringAsync method completes
我 trying/wanting 等待异步任务完成这就是我正在尝试做的事情
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");
那么如何实现呢?我曾尝试使方法异步然后等待它,但这没有帮助
不知道WebClient.UploadStringTaskAsync
comes with Microsoft.Bcl.Async for Windows Phone 8, but in case someone is wondering how it is implemented and how to use TaskCompletionSource
, here it is its implementation, taken from 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;
}
您可以使用回调来完成。如何定义回调取决于您。作为示例,我将其定义为一个带有两个参数的 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");
}
改用 UploadStringTaskAsync 方法
这将 return 一个您可以等待的任务。
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);
}
这是我如何实现它的解决方案
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");
我 trying/wanting 等待异步任务完成这就是我正在尝试做的事情
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");
那么如何实现呢?我曾尝试使方法异步然后等待它,但这没有帮助
不知道WebClient.UploadStringTaskAsync
comes with Microsoft.Bcl.Async for Windows Phone 8, but in case someone is wondering how it is implemented and how to use TaskCompletionSource
, here it is its implementation, taken from 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;
}
您可以使用回调来完成。如何定义回调取决于您。作为示例,我将其定义为一个带有两个参数的 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");
}
改用 UploadStringTaskAsync 方法
这将 return 一个您可以等待的任务。
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);
}
这是我如何实现它的解决方案
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");