windows phone 7 - How to get async result in main thread -


i'm making login page on windows phone 7 app. i'd login error status code on login page when login error message return server on async thread.

so question : in bellow code sample, please let me know how "responsestring(string)" in main method?

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx

using system; using system.net; using system.io; using system.text; using system.threading;  class httpwebrequestbegingetrequest {     private static manualresetevent alldone = new manualresetevent(false);      public static void main(string[] args)     {           // create new httpwebrequest object.         httpwebrequest request = (httpwebrequest)webrequest.create("http://www.contoso.com/example.aspx");          request.contenttype = "application/x-www-form-urlencoded";          // set method property 'post' post data uri.         request.method = "post";          // start asynchronous operation         request.begingetrequeststream(new asynccallback(getrequeststreamcallback), request);          // keep main thread continuing while asynchronous         // operation completes. real world application         // useful such updating user interface.          alldone.waitone();          /* i'd "responsestring" here. */     }      private static void getrequeststreamcallback(iasyncresult asynchronousresult)     {         httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate;          // end operation         stream poststream = request.endgetrequeststream(asynchronousresult);          console.writeline("please enter input data posted:");         string postdata = console.readline();          // convert string byte array.         byte[] bytearray = encoding.utf8.getbytes(postdata);          // write request stream.         poststream.write(bytearray, 0, postdata.length);         poststream.close();          // start asynchronous operation response         request.begingetresponse(new asynccallback(getresponsecallback), request);     }      private static void getresponsecallback(iasyncresult asynchronousresult)     {         httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate;          // end operation         httpwebresponse response = (httpwebresponse)request.endgetresponse(asynchronousresult);         stream streamresponse = response.getresponsestream();         streamreader streamread = new streamreader(streamresponse);         string responsestring = streamread.readtoend(); /* i'd responsestring in main method. */         console.writeline(responsestring);         // close stream object         streamresponse.close();         streamread.close();          // release httpwebresponse         response.close();         alldone.set();     } } 

you define responsestring class-level variable instead of defining within getresponsecallback method. way, can accessed anywhere in class, rather method scope.

to navigate page background thread, can use dispatcher.

    //method move next page. can called getresponsecallback     private void navigatetonextpage()     {         dispatcher.begininvoke(() =>         {            navigationservice.navigate(new uri("page2.xaml", urikind.relative"));         });     } 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -