c# - Async Await code. Why it is not working? -


Whether the simple code is valid for checking URL / website name. This code (sync code) works right when I comment on the sync code and cancel the code as async , the code is not working properly. What's wrong with my async programming?

  class program {static stringbirders urlStatus = new StringBuilder (""); // Public stable async work & lt; String & gt; GetHeadersAsync (string url) // async code public static string GetHeadersAsync (string URL) //// sync code {var request = (HttpWebRequest) WebRequest.Create (url); request. Method = "head"; Use {//} as the HTTPAPRSpuspace (use var response = escache). // Using uncompressed code (var response = request. Gate Response () as HTTP) //// Sync code {if (response! = Null & amp; Amp; response.StatusCode == HttpStatusCode.OK) "HTTP_OK" return; Return "error"; }} Hold (WebExation Pre) {return ex.ToString (); } Hold (Exception pre) {return ex.ToString (); }} Fixed zero main (string [] args) {string line Data; // Content of URLList.txt //http://www.oracle.com //http://www.oracletest.com //http://www.cnn.com //http://www.cnntest com (StreamReader sr = New StreamReader ("URLList.txt")} {while ((DataData = sr.ReadLine ())! = Null {var urlRetStatus = GetHeadersAsync (lineData); UrlStatus.Append (Line Data + urlRetStatus .toString () + System.Environment.NewLine);}} System.IO.File.WriteAllText (@ "ErrorURL.txt", urlStatus.ToString ());}}    

When you call a async method, a function that represents the asynchronous operation. Come to get real results from Wait Use keywords:

  Tasks  function = GetHeadersAsync (line data); String result = awaiting action;   

In your case, you are calling that method with the main which can not be a async method and can not be used / code > Waiting for so you have to use the work. Result property:

  work & lt; String & gt; Task = GetHeadersAsync (Line Data); String result = action. result;   

Keep in mind that until the async operation is complete, the calling detector will be blocked, so it is not recommended.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -