-
August 31st, 2015, 08:32 AM
#1
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!
-
August 31st, 2015, 02:38 PM
#2
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
-
August 31st, 2015, 03:48 PM
#3
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.
-
August 31st, 2015, 09:48 PM
#4
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.
-
September 1st, 2015, 10:18 AM
#5
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
-
September 1st, 2015, 08:10 PM
#6
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.
-
September 2nd, 2015, 07:22 AM
#7
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
-
September 2nd, 2015, 01:47 PM
#8
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.
-
September 3rd, 2015, 04:44 PM
#9
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.
-
September 7th, 2015, 09:30 AM
#10
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.
-
September 8th, 2015, 03:40 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|