c# - Calling GetRequestStream() hangs WPF UI for 1sec -


i have created wpf application has controls 1 label , 2 butons start , stop timers.

i have created 2 dispatchertimers 1 timer fire after every 50 milliseconds , update lable count value. dispatchertimer fire after every 2 seconds post data http post test server called "http://posttestserver.com/post.php?dir=test". in timer using getrequeststream causes ui stop 1 sec data posting posttestserver. have used following code.

public partial class mainwindow : window {

    static int count = 0;     dispatchertimer dispatchtimer = null;     dispatchertimer webdispatchtimer = null;     public mainwindow()     {         initializecomponent();         dispatchtimer = new dispatchertimer();         webdispatchtimer = new dispatchertimer();         webdispatchtimer.interval = timespan.frommilliseconds(2000);         dispatchtimer.interval = timespan.frommilliseconds(50);         dispatchtimer.tick += new eventhandler(dispatchtimer_tick);         webdispatchtimer.tick += new eventhandler(senddata_tick);     }      private void button1_click(object sender, routedeventargs e)     {         webdispatchtimer.start();         dispatchtimer.start();     }     private void dispatchtimer_tick(object sender, eventargs args)     {         count += 1;         label1.content = count.tostring();     }     private void senddata_tick(object sender, eventargs args)     {         string postdata = "this test posts string web server\n";         try         {             httpwebrequest request = (httpwebrequest)webrequest.create("http://posttestserver.com/post.php?dir=abcd");             request.method = "post";             request.protocolversion = httpversion.version11;             byte[] bytearray = encoding.utf8.getbytes(postdata);             request.contenttype = "application/x-www-form-urlencoded";             request.contentlength = bytearray.length;             request.proxy = null;             using (stream datastream = request.getrequeststream())             {                 datastream.write(bytearray, 0, bytearray.length);                 datastream.flush();                 datastream.close();             }             var response = request.getresponse() httpwebresponse;             response.close();             request.abort();             request = null;         }         catch (webexception ex)         {             messagebox.show(ex.message);             webdispatchtimer.stop();          }         catch (exception ex)         {             messagebox.show(ex.message);             webdispatchtimer.stop();         }              }     private void button2_click(object sender, routedeventargs e)     {         webdispatchtimer.stop();         dispatchtimer.stop();     } } 

i don't want ui hang 1 sec. made mistake ? me.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -