c# - how to create loading window while web conncetion is checking in windows form app? -
i have test web connection form in c#. want show loading window while connection being checked, , show result of checking.
this code testing web connection:
public bool connectionavailable(string strserver) { try { httpwebrequest reqfp = (httpwebrequest)httpwebrequest.create(strserver); httpwebresponse rspfp = (httpwebresponse)reqfp.getresponse(); if (httpstatuscode.ok == rspfp.statuscode) { // http = 200 - internet connection available, server online rspfp.close(); return true; } else { // other status - server or connection not available rspfp.close(); return false; } } catch (webexception) { // exception - connection not available return false; } }
and this:
private void button1_click(object sender, eventargs e) { string url = "web-url"; label1.text = "checking ..."; button1.enabled = false; if (connectionavailable(url)) { webclient w = new webclient(); w.headers[httprequestheader.contenttype] = "application/x-www-form-urlencoded"; label1.text = w.uploadstring(url, "post", "sn=" + textbox1.text); button1.enabled = true; } else { label1.text = "conntion fail"; button1.enabled = true; } }
on windows forms application user interface runs on 1 thread, if try run long running process, checking web connection might end being cause form freeze until completes work.
so, i'd start new thread check. raise event return result. while that's happening can user interface, such loading graphic, or allow user continue using features don't require internet connection.
create eventargs class of own can pass result:
public class connectionresulteventargs : eventargs { public bool available { get; set; } }
then in form class, create event, handlers , method action when event arrives
//create event , handler public delegate void connectionresulteventhandler(object sender, connectionresulteventargs e); public event connectionresulteventhandler connectionresultevent; //method run when event has been receieved, include delegate in case try interact ui thread delegate void connectionresultdelegate(object sender, connectionresulteventargs e); void connectionresultreceived(object sender, connectionresulteventargs e) { //check if request has come seperate thread, if raise exception unless invoke. if (invokerequired) { begininvoke(new connectionresultdelegate(connectionresultreceived), new object[] { this, e }); return; } //do stuff if (e.available) { label1.text = "connection good!"; return; } label1.text = "connection bad"; }
subscribe event when form loads:
private void form1_load(object sender, eventargs e) { //subscribe the results event. connectionresultevent += connectionresultreceived; }
and setup worker thread:
//check connection void begincheck() { try { httpwebrequest reqfp = (httpwebrequest)httpwebrequest.create("http://google.co.uk"); httpwebresponse rspfp = (httpwebresponse)reqfp.getresponse(); if (httpstatuscode.ok == rspfp.statuscode) { // http = 200 - internet connection available, server online rspfp.close(); connectionresultevent(this, new connectionresulteventargs {available = true}); } else { // other status - server or connection not available rspfp.close(); connectionresultevent(this, new connectionresulteventargs { available = false }); } } catch (webexception) { // exception - connection not available //raise event - connection false connectionresultevent(this, new connectionresulteventargs { available = false }); } } private void button1_click(object sender, eventargs e) { //loading graphic, screen or whatever label1.text = "checking connection..."; //begin checks - start in new thread thread t = new thread(begincheck); t.start(); }
Comments
Post a Comment