locked
XMLHTTPRequest2 callbacks, which thread?

    Question

  • Some questions about XMLHTTPRequest2 and its callback:

    1. Which thread is used in the calls to XMLHTTPRequest2Callback?

    2. Can I manipulate XAML elements (e.g. set status textblocks, etc.) in those callbacks?

    3. Can I do significant processing of the received data in OnResponseReceived?

    4. Can there be multiple XMLHTTPRequest2 operations active simultaneously?

    5. Can I call XHDR->Send(url) in my UI thread? I know I can but is there the potential for blocking the UI thread?

    My Metro app needs to download data every few minutes from various websites. It processes the received data before displaying it. In my Win32 app I simply fire off a low priority background thread at startup that does all this via synchronous calls to WinInet. It runs for the lifetime of the app and takes basically zero CPU time. For whatever reason, MSFT nuked both of those Win32 features (threads and synchronous WinInet) in Metro so I have to find another way.

    Thanks

    Friday, August 31, 2012 4:58 AM

Answers

  • Hi,henador, first, I notice that the XMLHTTPRequest2Callback is used in the thread which your XHDR->Send(url) is called, as you can view the thread info in the debug mode.and second , the HttpClient sample manipulates XAML elements in a continued task, may be because you should not do much heavily operations in one of those XMLHTTPRequest2Callbacks.third ,as mentioned already, forth,yes you can, in my app I used four or more XMLHTTPRequest2 object almost at the same time.fifth, I use another thread to do downloading and all the Send method is called in that seperate thread.

    and last, the HttpClient sample is here http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664 hope this can help you .


    • Edited by ahzhe Friday, August 31, 2012 11:08 AM
    • Proposed as answer by Jesse Jiang Monday, September 3, 2012 8:37 AM
    • Marked as answer by Jesse Jiang Wednesday, September 5, 2012 7:23 AM
    Friday, August 31, 2012 11:05 AM

All replies

  • Hi,henador, first, I notice that the XMLHTTPRequest2Callback is used in the thread which your XHDR->Send(url) is called, as you can view the thread info in the debug mode.and second , the HttpClient sample manipulates XAML elements in a continued task, may be because you should not do much heavily operations in one of those XMLHTTPRequest2Callbacks.third ,as mentioned already, forth,yes you can, in my app I used four or more XMLHTTPRequest2 object almost at the same time.fifth, I use another thread to do downloading and all the Send method is called in that seperate thread.

    and last, the HttpClient sample is here http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664 hope this can help you .


    • Edited by ahzhe Friday, August 31, 2012 11:08 AM
    • Proposed as answer by Jesse Jiang Monday, September 3, 2012 8:37 AM
    • Marked as answer by Jesse Jiang Wednesday, September 5, 2012 7:23 AM
    Friday, August 31, 2012 11:05 AM
  • Some more information on this topic. I've been looking into exactly how this works and found that there are differences in the behavior depending on the thread in which Send() is called.

    If you call XHR2->Send() from your UI thread, all the callbacks come in on the UI thread. This means that you can't call WaitForSingleObject in your UI thread on a hEvent triggered in the callback (your app hangs because the UI thread is busy in WaitForSingleObject, so the callbacks never happen and the Wait times out). Note that I ran into this when testing my new downloader code, I wasn't going to actually use Wait in the UI thread in the program.

    If you call XHR2->Send() from a TimeSliced threadpool thread, all the callbacks come in on a different thread. Specifically, in the debugger my calling thread is from ThreadPoolWinRT.dll while the callback thread is a different one that came from NTDLL.dll. This means that you *can* call XHR2->Send() and then WaitForSingleObject to wait for the response.

    If you call XHR2->Send() from a non-TimeSliced threadpool thread, all the callbacks come in on a different thread. In the debugger my calling thread is from NTDLL.dll add the callback thread is a different one from NTDLL.dll. As with the TimeSliced ThreadPool scenario, this means that you *can* call XHR2->Send() and then WaitForSingleObject to wait for the response.

    Anyway, that's what I've found after playing with this for a while. There's a clear divide between calling XHR2->Send() on a UI thread vs. a threadpool thread.

    Monday, September 17, 2012 4:21 PM
  • New in Windows 8.1: the Windows.Web.Http HttpClient API.  This is an easy-to-use, full-featured HTTP API that works in all languages: C++, .NET (C#, VB) and JavaScript.    Advantages of the new API include: strongly typed headers (meaning: you write fewer bugs in less time), full support for standard WinRT async concepts and data types.

    HttpClient also lets you inject your modular filter code into the HTTP processing pipeline, letting you handle logging, testing, retry and auth more naturally.  Samples of filters include retry and metered network filters in the HttpClient sample, and OAUTH and OAUTH 2.0 support in the Web Authentication Broker sample.

    (Note that the existing IXHR2 code is still available for developers, and your existing Windows 8 code should continue to work as expected)

    There's a //build/ 2013 video for the new API at http://channel9.msdn.com/Events/Build/2013/4-092

    We also have samples at http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664

    There's an OAUTH 0 filter sample at http://code.msdn.microsoft.com/windowsapps/Web-Authentication-d0485122

    And there's documentation at http://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.aspx


    Network Developer Experience Team (Microsoft)

    Tuesday, July 16, 2013 9:28 PM