How can a synchronous WinHttp request be cancelled?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 11:11:27

问题


My service has a thread that may potentially be executing a WinHttpSendRequest when someone tries to stop my service.

The WinHttpCloseHandle documentation says:

An application can terminate an in-progress synchronous or asynchronous request by closing the HINTERNET request handle using WinHttpCloseHandle

But, then later on the same documentation seems to contradict this. It says:

An application should never WinHttpCloseHandle call on a synchronous request. This can create a race condition.

I've found this blog post that seems to agree I can't call WinHttpCloseHandle.

I'm wondering how can I cancel this operation so that my service can be stopped gracefully? I can't really wait for the WinHttpSendRequest to timeout naturally because it takes too long and my service doesn't stop quickly enough. I think windows reports this as an error and then forcefully kills the service in a shutdown.

An ideas would be appreciated.


回答1:


Calling WinHttpCloseHandle off a background thread to force handle close is perhaps not the best solution. Still it works, and the original caller would receive something like "bad handle" error code and the request would be forcefully terminated.

It would be possibly unsafe to abuse this power, and one would rather implement async requests instead. However, in case of shutting down stale service it is going to work fine.



来源:https://stackoverflow.com/questions/8939316/how-can-a-synchronous-winhttp-request-be-cancelled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!