ahmd0
ahmd0

Reputation: 17293

Ways to speed up termination of socket WinHttp APIs using C++ for Windows

I have a Windows service application written in C++/MFC that establishes an HTTP connection via Windows sockets. It employs WinHttp* APIs to do all the socket related operations. It all works great, except when someone tries to stop/uninstall that service if it's in the process of connecting via the socket (espeically in a situation when such connection could not be established, like, say, if my Internet cable is unplugged) those socket APIs do not return for several seconds before the service can close (obviously, the mechanism inside the service waits for my socket thread to finish "gracefully".)

My question is, if I have an open socket handle, as well connection and request handles (of HINTERNET type), obtained from a call to WinHttpOpen(), WinHttpConnect() and WinHttpOpenRequest(), is there any way to forcefully make any other WinHttp* APIs that may be currently working with the socket to return immediately?

PS. I was thinking to do so from a main thread before it begins waiting for the socket thread to close (before uninstalling/stopping the service.)

Upvotes: 0

Views: 1140

Answers (2)

Roman Ryltsov
Roman Ryltsov

Reputation: 69662

You can close your WinHTTP handle from another thread. This breaks the blocking call on the main thread with the delay you are trying to avoid. See How can a synchronous WinHttp request be cancelled?

Upvotes: 1

Martin James
Martin James

Reputation: 24857

[obviously, the mechanism inside the service waits for my socket thread to finish "gracefully'] - if you know the cause of the problem, why not remove it?

Why are you waiting?

Upvotes: 0

Related Questions