Threading issue

M

Mike23

Hi there,

I posted this question earlier using a different subject line but after more
research I realized my original assessment was incorrect. I'm invoking
"System.Web.Services.Protocols.SoapHttpClientProtocol.InvokeAsync()" on the
same thread as "Page_Load()". This function takes a callback via the 3rd arg
but the callback isn't being called for some reason until the "Page_Load()"
thread exits (the callback occurs on another thread). Outside of ASP.NET
however (in a console test app I created), the same callback is invoked as
expected (in parallel with the main thread). Can anyone shed any light on
what's going on here. Thanks.
 
B

bruce barker

your are confused about async calls. they occur on the invoking thread,
and the callback is to the same thread. as async calls involve an os i/o
call, the thread can continue to process while the os does the i/o. when
the i/o completes, the callback is done to the same thread.

in asp.net, if you make an async call, you should wait for completion
before returning the request thread to the pool. as asp.net can switch
threads during processing (for diffent event callbacks), this means the
callback can occur during processing to a different request. asp.net may
even shut done the thread before the callback due to pool timeout or
Response.Redirects. to avoid threading issues, you have 3 options:

1) during an event processing (onload, onclick, etc). make the async
call, then stall the thread until completion.

2) use the built-in async processing for webforms
(Page.RegisterAsyncTask). it will keep the thread assigned to the
request, and wait for completion (before prerender).

3) start a new dedicated thread to process the async task.

-- bruce (sqlwork.com)
 
B

bruce barker

just saw your above thread.

the point of async i/o processing is to avoid the overhead of a second
thread. while the i/o is be handled by the os, the invoking thread can
continue to do other work. often you can do overlapped i/o. say start
several i/o's at the same time (or before previous have completed).

this works real well with webservice calls. say you needed to call 3
different webservices to process a request, you can make all three calls
at the same time, wait on completion, then continue processing the
request, all with one thread.

if you goal is background processing then you should use a pooled worker
threads to which you can pump messeages, or start one background thread
and have it do async calls. just implement a queue and use mutex's to
schedule requests.

-- bruce (sqlwork.com)
 
M

Mike23

the point of async i/o processing is to avoid the overhead of a second
thread. while the i/o is be handled by the os, the invoking thread can
continue to do other work. often you can do overlapped i/o. say start
several i/o's at the same time (or before previous have completed).

this works real well with webservice calls. say you needed to call 3
different webservices to process a request, you can make all three calls
at the same time, wait on completion, then continue processing the
request, all with one thread.

if you goal is background processing then you should use a pooled worker
threads to which you can pump messeages, or start one background thread
and have it do async calls. just implement a queue and use mutex's to
schedule requests.

Thanks for the feedback. It's actually a web service I'm calling using the
event-based asynchronous pattern. That is, I'm calling the web service's
"MethodNameAsync()" method multiple times on the "Page_Load()" thread and my
callback should be invoked in parallel on another thread. Up until 5 minutes
ago I thought my callback wasn't being invoked at all until the
"Page_Load()" thread itself exited (which I don't want it to do - I block
that thread when it's finished its own work until my callback signals that
all requests have been completed). Apparently my callback is being called
however (in parallel with "Page_Load()") because if I cancel all the web
service requests on the "Page_Load()" thread using the service's
"CancelAsync()" method, then my callback is immediately invoked as expected
(in parallel with "Page_Load()"). Based on that and some other testing I
just conducted, it appears what's happening may not be a threading issue at
all. It simply looks like when I call "MethodNameAsync()", it's queueing up
all the calls but it can't send them off to the remote server for processing
until I allow the "Page_Load()" thread to exit. Something's blocking it from
sending those requests IOW, hence the reason my callback isn't being invoked
until "Page_Load()" exits (since the requests simply haven't been serviced
yet). The same problem doesn't occur outside of ASP.NET however. This is a
very straight-forward task I'm attempting however (calling a remote web
service) so why can't the request be sent until "Page_Load()" actually
exits. Any ideas would be greatly appreciated. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top