【问题标题】:Task status :Waiting for activation -DownloadStringTaskAsync -WP8任务状态:等待激活 -DownloadStringTaskAsync -WP8
【发布时间】:2014-02-06 14:45:51
【问题描述】:

任务的状态总是“等待激活”。任务的结果=“”。 我不明白为什么...感谢您的帮助 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;
        }
   } 
}     

【问题讨论】:

  • 您似乎遇到了死锁,您的任务无法启动。能不能多加点代码,特别是GetXml方法是怎么调用的?
  • 这段代码甚至无法编译,你有try 没有catch/finally。显示其余部分。
  • 我只写了我认为与问题有关的代码...但现在我添加了问题...
  • 是的,我知道这篇文章...我尝试了 ConfigureAwait(false),但还是一样...

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


【解决方案1】:

我所有的方法都需要异步。

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

【讨论】:

    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多