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 When you call a In your case, you are calling that method with the Keep in mind that until the 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 ());}}
async method, a function that represents the asynchronous operation. Come to get real results from
Wait Use keywords:
Tasks
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;
async operation is complete, the calling detector will be blocked, so it is not recommended.
Comments
Post a Comment