Skip to main content

Answered Question Worker thread(BeginGetRequestStream,BeginGetResponse) both syncRSS Feed

(0)

XASD
XASD

Member

Member

91 points

55 Posts

Worker thread(BeginGetRequestStream,BeginGetResponse) both sync

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
What could be wrong?Is there any difference between BeginGetRequestStream/BeginGetResponse thread usage?

Thanks.

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25058 points

2,749 Posts

Re: Worker thread(BeginGetRequestStream,BeginGetResponse) both sync

See http://silverlight.net/forums/t/18336.aspx

 

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

jackbond
jackbond

Contributor

Contributor

2896 points

745 Posts

Re: Worker thread(BeginGetRequestStream,BeginGetResponse) both sync

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.

XASD
XASD

Member

Member

91 points

55 Posts

Re: Worker thread(BeginGetRequestStream,BeginGetResponse) both sync

Please,clarify a little what does it means EndGetRequestStream()-«something needs to be done on the UI thread.»

[CODE]
using (var rs = r.EndGetRequestStream(a_r)) { rs.Write(...); a_r= r.BeginGetResponse(x => re.Set(), null); } re.WaitOne();//hangs forever
[/CODE]
So,you say particularly that BeginGetResponse start something can be done only in UI thread("AsyncHelper.BeginOnUI->UiSynchronizationContext.Current.Send()",why not Post???),but not end this "something" before re.WaitOne() stop it? Who is stopped EndGetRequestStream or BeginGetResponse from using UI thread by re.WaitOne();//hangs forever line? Both (BeginGetRequestStream/BeginGetResponse) start in "worker thread",but first work(if first statement in callback delegate is re.Set()) and latter just hangs because of "re.WaitOne()". Is it combination of EndGetRequestStream/BeginGetResponse makes deadlock in this case? EndGetRequestStream() forget something "needs to be done" in UI thread for successful BeginGetResponse()?

Thanks a lot.

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25058 points

2,749 Posts

Re: Re: Worker thread(BeginGetRequestStream,BeginGetResponse) both sync

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.

 

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

XASD
XASD

Member

Member

91 points

55 Posts

Re: Re: Worker thread(BeginGetRequestStream,BeginGetResponse) both sync

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.

jackbond
jackbond

Contributor

Contributor

2896 points

745 Posts

Answered Question

Re: Re: Worker thread(BeginGetRequestStream,BeginGetResponse) both sync

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.

XASD
XASD

Member

Member

91 points

55 Posts

Re: Re: Re: Worker thread(BeginGetRequestStream,BeginGetResponse) both sync

Thanks a lot,I see:-)

  • Unanswered Question
  • Answered Question
  • Announcement