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); } }
source share