C # How to stop a method if it takes more than 2 seconds?

The following program will connect to the network and receive the html content of the msnbc.com web page and print the result. If it takes more than 2 seconds to retrieve data from a web page, I want my method to stop working and return. Could you tell me how I can do this with an example?

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { gethtml(); MessageBox.Show("End of program"); } public void gethtml() { HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create("http://msnbc.com/"); WebResponse Response = WebRequestObject.GetResponse(); Stream WebStream = Response.GetResponseStream(); StreamReader Reader = new StreamReader(WebStream); string webcontent = Reader.ReadToEnd(); MessageBox.Show(webcontent); } } 
+6
source share
5 answers

As stated above .Timeout

  public void gethtml() { HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create("http://msnbc.com/"); WebRequestObject.Timeout = (System.Int32)TimeSpan.FromSeconds(2).TotalMilliseconds; try { WebResponse Response = WebRequestObject.GetResponse(); Stream WebStream = Response.GetResponseStream(); StreamReader Reader = new StreamReader(WebStream); string webcontent = Reader.ReadToEnd(); MessageBox.Show(webcontent); } catch (System.Net.WebException E) { MessageBox.Show("Fail"); } } 
+4
source

Two seconds is too long to block the user interface. You should only block the user interface if you plan to get a result of, say, fifty milliseconds or less.

Read this article on how to make a web request without blocking the user interface:

http://www.developerfusion.com/code/4654/asynchronous-httpwebrequest/

Please note that this will all be much easier in C # 5, which is currently in beta. In C # 5, you can simply use the await operator to asynchronously wait for the result of a task. If you want to see how this will work in C # 5, see

http://msdn.microsoft.com/en-us/async

+12
source

Set the Timeout property of your WebRequest object. Documentation

MSDN example:

 // Create a new WebRequest Object to the mentioned URL. WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com"); Console.WriteLine("\nThe Timeout time of the request before setting is : {0} milliseconds",myWebRequest.Timeout); // Set the 'Timeout' property in Milliseconds. myWebRequest.Timeout=10000; // This request will throw a WebException if it reaches the timeout limit before it is able to fetch the resource. WebResponse myWebResponse=myWebRequest.GetResponse(); 
+6
source

You can use the TimeOut property in HttpWebRequest

+2
source

Consider switching to asynchronous content loading. You will stop blocking the UI thread and you can easily handle multiple requests. You can significantly increase the timeout without affecting the user interface and you can decide to receive a response if you still want to receive data.

0
source

Source: https://habr.com/ru/post/914352/


All Articles