2016-06-21 102 views
-1

我正在使用Windows Phone應用程序。在執行以下函數時,執行以下函數: HttpResponseMessage response = await client.GetAsync(connection1).ConfigureAwait(false); 它跳過代碼的其餘部分,然後控制轉到父函數並執行其餘代碼並再次返回到此函數。如何解決這個問題?異步函數調用問題

public async void vehicleValidation()  
{ 
    //isValidVehicle = true; 
    var client = new System.Net.Http.HttpClient(); 
    //string connection = "http://mymlcp.co.in/mlcpapp/get_slot.php?vehiclenumber=KL07BQ973"; 
    string connection1 = string.Format("http://mymlcp.co.in/mlcpapp/?tag=GetIsValidUser&employeeId={0}&name={1}",txtVeh.Text,"abc"); 
    HttpResponseMessage response = await client.GetAsync(connection1).ConfigureAwait(false); 
    //var response = await client.GetAsync(connection1); 
    // HttpResponseMessage response = client.GetAsync(connection1).Result; 
    var cont = await response.Content.ReadAsStringAsync(); 
    var floorObj = Newtonsoft.Json.Linq.JObject.Parse(cont); 
    //var resp = await (new MLCPClient()).GetIsValidUser(txtVeh.Text, "xyz"); 

    if (String.IsNullOrEmpty(floorObj["error"].ToString()) || floorObj["error"].ToString().Equals("true")) 
    { 
     isValidVehicle = false; 
    } 
    else 
    { 

     isValidVehicle = true; 
    } 
} 
+0

注的最佳做法介紹的是*正是*你應該期待什麼異步方法。這是一點。 –

+0

我不認爲你應該使用'ConfigureAwait(false)',因爲它引發了'同步'問題。 – Venky

+0

@jonSkeet你能解釋一下嗎? –

回答

1

你永遠不應該有async void,除非你正在編寫一個事件處理程序,你需要讓你的功能在你的父函數返回一個Task然後await功能。

讀「Async/Await - Best Practices in Asynchronous Programming」上從未像現在這樣做async void,使你的代碼「異步一路」當你需要等待一些控制被返回給調用者

+0

命名方法時也應該使用PascalCase。 –