CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: WinInet callback not called when the main thread sleeps

  1. #1
    Join Date
    Apr 2011
    Posts
    51

    WinInet callback not called when the main thread sleeps

    Hi, All,

    I tried to use WinInet API to send a Http request and get back a page asynchronously, with the main thread waiting until the response is received in the callback function. However, it does not seem to work because the callback function is never invoked if the main thread sleeps. Anyone has some idea what is going on? If the main thread does not wait sleeping, it works fine.

    Thanks at lot!

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: WinInet callback not called when the main thread sleeps

    Asynchronous Example Application has main thread sleeping (well, waiting for event actuallly) alright.
    Best regards,
    Igor

  3. #3
    Join Date
    Apr 2011
    Posts
    51

    Re: WinInet callback not called when the main thread sleeps

    Igor, thanks for your suggestion. I tried WaitForSingleObject instead of my own waiting function, but I got the same problem, that is, StatusCallBack function did not get called and WaitForSingleObject ended up with timing out. I don't understand why the StatusCallbacck thread is affected by WaitForSingleObject or Sleep of the main thread. If the main thread does not sleep or WaitForSingleObject, StatusCallback seems work fine. Thanks.
    Last edited by caperover2000; August 31st, 2015 at 09:14 PM.

  4. #4
    Join Date
    Apr 2011
    Posts
    51

    Re: WinInet callback not called when the main thread sleeps

    This link suggests not to WaitForSingleObject the request completion event

    http://stackoverflow.com/questions/3...ect-error-1008

    I don't quite understand why. Anyone has some idea? Thanks.

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: WinInet callback not called when the main thread sleeps

    I have only one theory that would explain the behavior. Callback may be called by a window message handler. However, I found no mentioning of window messages in the articles on WinInet functions. Try switching from WaitForSingleObject to MsgWaitForMultipleObjects and see if it helps.
    Best regards,
    Igor

  6. #6
    Join Date
    Apr 2011
    Posts
    51

    Re: WinInet callback not called when the main thread sleeps

    Replacing WaitForSingleObject with MsgWaitForMultipleObjects does not help. Is there any way to find out why a thread is not moving? Thanks.

  7. #7
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: WinInet callback not called when the main thread sleeps

    Just a guess, maybe it is IOCP-based or something underneath, and your object is already being waited on by those internals.
    http://stackoverflow.com/questions/2...nt-handle-in-c

    Try using a secondary event object (CreateEvent) and wait on that, and in your callback signal the object (SetEvent) to notify the main thread.
    Nobody cares how it works as long as it works

  8. #8
    Join Date
    Apr 2011
    Posts
    51

    Re: WinInet callback not called when the main thread sleeps

    Thanks for your suggestion Zerver. Since the main thread is the only one that waits on the event for the first time, there should be nothing else, especially the callback, waiting on the event object, but I will definitely try.

  9. #9
    Join Date
    Apr 2011
    Posts
    51

    Re: WinInet callback not called when the main thread sleeps

    Interesting, as soon as WaitForSingleObject times out in the main thread and does not sleep anymore, status callback starts to be invoked. Don't understand why the callback thread is blocking when the main thread sleeps.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: WinInet callback not called when the main thread sleeps

    if your main thread Sleep()s, or is blocked with WaitForSingleObject().

    Then it can't do anything else, so no callbacks can get called at all. This is elementary windows behaviour.

    It's also a bad idea to have your main thread blocking for any reason. THe idea is that you simply return to the messageloop, and handle the callbacks as they come.

    if you need periodic checking of the I/O other than that provided by the callbacks, then install a timer and handle the timer messages as well.

  11. #11
    Join Date
    Apr 2011
    Posts
    51

    Re: WinInet callback not called when the main thread sleeps

    That is true, but it is supposed to work by design. For example, the example by MS that Igor mentioned above. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)