Skip to main content
Microsoft Silverlight
Home Forums Silverlight Programming Programming with .NET - General Worker thread(BeginGetRequestStream,BeginGetResponse) both sync
7 replies. Latest Post by XASD on June 20, 2008.
(0)
XASD
Member
91 points
55 Posts
06-16-2008 9:28 AM |
Following code hangs forever,without visible reason,docs says HttpWebRequest utilize "worker thread" to do it's work,but it seems block UI thread anyway:
AutoResetEvent re = new AutoResetEvent(false); IAsyncResult a_r= null; a_r = r.BeginGetRequestStream(x => re.Set(), null); re.WaitOne();//OK using (var rs = r.EndGetRequestStream(a_r)) { rs.Write(...); a_r= r.BeginGetResponse(x => re.Set(), null); } re.WaitOne();//hangs forever
Thanks.
Yi-Lun L...
All-Star
25058 points
2,749 Posts
06-18-2008 4:14 AM |
See http://silverlight.net/forums/t/18336.aspx
jackbond
Contributor
2896 points
745 Posts
06-18-2008 5:11 AM |
XASD:What could be wrong?
Your call to BeginGetResponse is queued after the completion of your method, so it is never made due to the WaitOne. You're clearly trying to block until the web service call completes, so I'll save you a bunch of time, give up. Can't be done.
06-18-2008 11:16 AM |
Please,clarify a little what does it means EndGetRequestStream()-«something needs to be done on the UI thread.»
Thanks a lot.
06-19-2008 7:08 AM |
Well, have a look at the code of EndGetResponse from Reflector:
AsyncHelper.BeginOnUI(delegate (object sendState) {response = this.InternalEndGetResponse(asyncResult);}, null);
As you can see, EndGetResponse will delegate the work to UI. If the UI thread is blocked by your "re", of course the browser will hang forever. So you must call re.Set before EndGetResponse.
06-19-2008 8:40 AM |
I'm talking about BeginGetResponse not EndGetResponse,why code above does not work? BeginGetResponse not have a chance to execute re.Set() in delegate.Why similar combination BeginGetRequestStream(x=>re.Set());re.WaitOne() work just fine and free UI thread for EndGetRequestStream(),but BeginGetResponse(x=>re.Set());re.WaitOne(); just hangs forever? EndGetResponse() will follow re.Wait(); and must have no problems with UI thread in my case.
... re.WaitOne();//hangs forever using (var rs = r.EndGetResponse(a_r))//must be executed just fine in my case,if previous line would not block "something" { }
Best wishes.
06-19-2008 1:39 PM |
If you can, set a break point in the web service method, and you'll realize what is going on, and this is key:
Web service calls are invoked on the UI thread.
So even though you are calling BeginGetResponse, which returns immediately (sure seems async), it's actual work doesn't happen until the UI thread picks it up(and in your case you've blocked it.) The bottom line is, you can't do what you are trying to do, which is to turn an async call into a sync call.
06-20-2008 3:55 PM |
Thanks a lot,I see:-)