相关疑难解决方法(0)

使用Task.wait()应用程序挂起并永不返回

我是C#的新手并且正在使用Task.我试图运行这个应用程序但我的应用程序每次都挂起.当我添加时task.wait(),它会一直等待,永远不会返回.任何帮助深表感谢.编辑:我想异步调用DownloadString.当我按照"Austin Salonen"的建议执行task.Start()时,我没有得到位置的地址,而是从returnVal获取位置字符串中的默认值.这意味着在任务完成之前,位置得到了值.如何确保在任务完成后只有位置被指定returnVal.

public class ReverseGeoCoding
        {
                static string baseUri = "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";
                string location = "default";
                static string returnVal = "defaultRet";
                string latitude = "51.962146";
                string longitude = "7.602304";
                public string getLocation()
                {
                    Task task = new Task(() => RetrieveFormatedAddress(latitude, longitude));  
                //var result = Task.Factory.StartNew(RetrieveFormatedAddress("51.962146", "7.602304"));
                    task.Wait();
                    //RetrieveFormatedAddress("51.962146", "7.602304");
                    location = returnVal;
                    return location;
                }
                public static void RetrieveFormatedAddress(string lat, string lng)
                {
                    string requestUri = string.Format(baseUri, lat, lng);

                    using (WebClient wc = new WebClient())
                    {
                        wc.DownloadStringCompleted …
Run Code Online (Sandbox Code Playgroud)

c# google-api task wait reverse-geocoding

9
推荐指数
2
解决办法
8897
查看次数

标签 统计

c# ×1

google-api ×1

reverse-geocoding ×1

task ×1

wait ×1