Request a url don't want to wait for a response
I need to hit a url but don't want to wait for a response. Just ask for url (cron) to update data. I am using this below code wait for a response. Please clarify.
string url = "http://exampleurl";
WebRequest request = HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();
+3
source to share
1 answer
Assuming you don't need an answer:
This line prevents the program from running the next line until the method completes:
WebResponse response = request.GetResponse();
Therefore, you can run this line as a separate task to avoid blocking your code. Your code will work even if the task (getResponse ()) is not finished yet.
string url = "http://exampleurl";
WebRequest request = HttpWebRequest.Create(url);
var t = Task.Run(() => request.GetResponse() );
See documentation for Task.Run () for better understanding.
Edit: If your running an older version of .net / C # task.run won't work, you can use Task.Factory.StartNew instead .
0
source to share