2016-12-16 1 views
0

WebClient를 사용하여 POST 요청을 보내려고하고 있으며 UI가 멈추는 것을 막기 위해 "UploadStringAsync"를 사용하고 있습니다. 나는 업로드가 완료된 후 변수를 반환 할,하지만 난 오류가 점점 오전 :uploadstring의 변수를 반환했습니다.

try 
{ 
    List<string> results = new List<string>(); 

    //Contact the API 
    using (WebClient getResults = new WebClient()) 
    { 

    ... 

     //Send the POST and get the result! 

     getResults.UploadStringCompleted += (sender, e) => 
     { 
      dynamic finalResult = JObject.Parse(e.Result);//finalResults.selectedProfile.name 
      results.Add(finalResult.selectedProfile.name); 
      return (results.toArray()); //Error is here 
     }; 

     getResults.UploadStringAsync(new Uri(URL), "POST", postInfo); 
    } 

    //This is what was there before: return (results.ToArray()); 
} 
catch (Exception ex) 
{ 
    ... 
} 

I 나타나는 오류는 다음과 같습니다 당신이 어떤 비동기 구조를 사용하려는 경우

Since 'System.Net.UploadStringCompletedEventHandler' returns void, a return keyword must not be followed by an object expression

+0

배열을 매개 변수로 사용하고 처리기 끝에서 호출하는 함수를 작성하지 않는 이유는 무엇입니까? – Brutus

답변

0

, 나는 희망을 먼저 아래와 같이 asyn 함수를 작성해야합니다.

static async Task<string> GetData() 
    { 
     using (WebClient webClient = new WebClient()) 
     { 
      return await webClient.UploadStringTaskAsync(new Uri("http://www.facebook.com/"), "POST", string.Empty); 
     } 
    } 

그런 다음 결과를 기다리고 구문 분석 할 수 있습니다.

Task<string> getDataTask = GetData(); 
    if (!string.IsNullOrEmpty(getDataTask.Result)) 
    { 
     List<string> results = new List<string>(); 
     dynamic finalResult = JObject.Parse(getDataTask.Result); 
     results.Add(finalResult.selectedProfile.name); 
    } 

그러나, 내가 생각하는 것은 우리가 UI 동결을 처리 할 때, WPF 응용 프로그램에서 예를 들어, 우리가 멀티 스레딩 ,하지만 asyc를 사용한다는 것입니다.

관련 문제