c#-任务状态:等待激活-DownloadStringTaskAsync -WP8
作者:互联网
任务的状态始终为“等待激活”.任务的结果=“”.
我不明白为什么…谢谢您的帮助
UI调用GetDocLibs方法.
public class ServerFunctions
{
public static List<BdeskDocLib> GetDocLibs(bool onlyDocLibPerso)
{
string xmlContent = GetXml();
List<BdeskDocLib> result = BdeskDocLib.GetListFromXml(xmlContent, onlyDocLibPerso);
return result;
}
private static String GetXml()
{
Task<String>task=requesteur.Query(dataRequestParam);
task.Wait();
xmlResult = task.Result;
return xmlResult;
}
}
public class DataRequest
{
public Task<String> Query(DataRequestParam dataRequestParam)
{
try
{
WebClient web = new WebClient();
if (!string.IsNullOrEmpty(dataRequestParam.AuthentificationLogin))
{
System.Net.NetworkCredential account = new NetworkCredential(dataRequestParam.AuthentificationLogin, dataRequestParam.AuthentificationPassword);
web.Credentials = account;
}
return web.DownloadStringTaskAsync(dataRequestParam.TargetUri).ConfigureAwait(false);
}
catch(WebException we)
{
MessageBox.Show(we.Message);
return null;
}
}
}
解决方法:
我所有的方法都必须异步.
public class ServerFunctions
{
public static async Task<List<BdeskDocLib>> GetDocLibs(bool onlyDocLibPerso)
{
string xmlContent = await GetXml();
List<BdeskDocLib> result = BdeskDocLib.GetListFromXml(xmlContent, onlyDocLibPerso);
return result;
}
private async static Task<String> GetXml()
{
Task<String>task=requesteur.Query(dataRequestParam);
task.Wait();
xmlResult = task.Result;
return xmlResult;
}
}
标签:asynchronous,async-await,task,windows-phone-8,c 来源: https://codeday.me/bug/20191122/2057426.html