我正在尝试/想要等到异步任务完成,这就是我正在尝试做的
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");
那么如何实现呢?我曾尝试使方法异步然后等待它,但这没有帮助
最佳答案
改用 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);
}
https://stackoverflow.com/questions/33116747/
相关文章:
bash - 将 Bash Shell 变量传递给 Awk NR 函数
jquery - VideoJS - 播放视频时,暂停所有其他视频
linux-kernel - BUILDROOT ip : can't find device et
vim - 如何在 Vim 中快速上下(ctrl u 和 ctrl d)重新映射到 ctrl j 和
drupal - 在新的 Drupal 8 站点上,子菜单链接没有出现?
c# - 如何从整数生成 MD5 哈希(32/64 个字符)
java - 在时区字符串列表中根据 GMT 时间对时区进行排序