如何取消同步 WinHttp 请求? [英] How can a synchronous WinHttp request be cancelled?

查看:41
本文介绍了如何取消同步 WinHttp 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务有一个线程,当有人试图停止我的服务时,该线程可能正在执行 WinHttpSendRequest.

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

WinHttpCloseHandle 文档 说:

应用程序可以通过使用 WinHttpCloseHandle

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:

应用程序不应WinHttpCloseHandle 调用同步请求.这会造成竞争条件.

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

我找到了 这篇博文 似乎同意我不能调用 WinHttpCloseHandle.

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

我想知道如何取消此操作以便可以正常停止我的服务?我真的不能等待 WinHttpSendRequest 自然超时,因为它花费的时间太长而且我的服务没有足够快地停止.我认为 Windows 将此报告为错误,然后在关闭时强行终止服务.

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.

推荐答案

从后台线程调用 WinHttpCloseHandle 来强制关闭句柄可能不是最好的解决方案.它仍然有效,原始调用者会收到类似坏句柄"错误代码的内容,并且请求将被强行终止.

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.

这篇关于如何取消同步 WinHttp 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆