Threading issue

Discussion in 'ASP .Net' started by Mike23, Nov 11, 2009.

  1. Mike23

    Mike23 Guest

    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.
     
    Mike23, Nov 11, 2009
    #1
    1. Advertising

  2. Mike23

    bruce barker Guest

    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)

    Mike23 wrote:
    > 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.
    >
    >
     
    bruce barker, Nov 11, 2009
    #2
    1. Advertising

  3. Mike23

    bruce barker Guest

    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)

    bruce barker wrote:
    > 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)
    >
    > Mike23 wrote:
    >> 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.
    >>
     
    bruce barker, Nov 11, 2009
    #3
  4. Mike23

    Mike23 Guest

    > 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.
     
    Mike23, Nov 11, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    3
    Views:
    4,260
  2. Edward Leibnitz

    Threading Issue ..

    Edward Leibnitz, Dec 6, 2003, in forum: Java
    Replies:
    5
    Views:
    390
    Edward Leibnitz
    Dec 7, 2003
  3. Replies:
    9
    Views:
    1,103
    Mark Space
    Dec 29, 2007
  4. Steven Woody
    Replies:
    0
    Views:
    454
    Steven Woody
    Jan 9, 2009
  5. Steven Woody
    Replies:
    0
    Views:
    469
    Steven Woody
    Jan 9, 2009
Loading...

Share This Page