在InternetOpenURL尝试(和失败)连接时,如何避免完全利用CPU或终止? [英] How to avoid full CPU utilisation, or terminate, while InternetOpenURL is trying (and failing) to connect?

查看:406
本文介绍了在InternetOpenURL尝试(和失败)连接时,如何避免完全利用CPU或终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的应用程序,它可以线程下载文件。该线程使用 WinINet API ,并这样开始:

I have a fairly simple application which downloads files in a thread. This thread uses the WinINet APIs, and begins like so:

    HINTERNET hInternet = InternetOpen(strUserAgent.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    DWORD dwFlags = INTERNET_FLAG_NO_UI | INTERNET_FLAG_HYPERLINK | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD;
    HINTERNET hUrl = InternetOpenUrl(hInternet, m_strURL.c_str(), L"", 0, dwFlags, NULL);

但是,如果没有Internet连接或远程主机已关闭,则 InternetOpenUrl 将需要很长时间才能完成。在执行此操作时(也就是说,仅在无法连接到远程主机的情况下),它将使用一个CPU的80-100%,直到最终返回。由于超时延迟设置,该操作通常可以持续一分钟左右。在一个系统上,尽管有超时设置(如下所示),但持续了十分钟。

However, if there is no internet connection, or the remote host is down, InternetOpenUrl will take a long time to time out and complete. While it is doing this - that is, only in the situation that it can't connect to the remote host - it will use between 80-100% of one CPU until it finally returns. This can often continue for a minute or so, because of the timeout delay setting. On one system, in spite of the timeout settings (noted below), this has continued for up to ten minutes.

我如何:


  • 只是在尝试连接时避免如此大量的CPU使用情况?

  • 给线程终止信号如果我想关闭该应用程序?通常,超时是可以的,但是如果应用程序需要关闭,则它将等待该线程完成,这将花费大量CPU在 InternetOpenUrl 内部做很少的事情。

  • Avoid such massive CPU usage when it's just trying to connect?
  • Signal the thread to terminate if I want to shut down the app? Normally a timeout is fine, but if the app needs to close then it will wait on this thread to finish, which is spending lots of CPU doing very little inside InternetOpenUrl.

[Aside:我的系统上的当前超时设置,如InternetQueryOption所示:

[Aside: Current timeout settings on my system, as revealed by InternetQueryOption:


  • INTERNET_OPTION_CONNECT_TIMEOUT :60s

  • INTERNET_OPTION_RECEIVE_TIMEOUT :30秒

  • INTERNET_OPTION_SEND_TIMEOUT :30秒

  • INTERNET_OPTION_CONNECT_TIMEOUT: 60s
  • INTERNET_OPTION_RECEIVE_TIMEOUT: 30s
  • INTERNET_OPTION_SEND_TIMEOUT: 30s

更改这些将减少方法放弃并返回之前的时间,因此减少了使用这么多CPU所花费的时间,但可能会影响连接-毕竟,超时对于原因。此应用程序可以用于奇怪的连接情况,例如在船上,在这种情况下,连接可能是通过卫星进行的,具有高延迟,并且比标准的桌面Internet连接花费的时间更长。我不知道合理的超时时间是多少。另外,必须有一个比简单地缩短超时时间更好的方法来避免CPU使用并更快地终止。]

Changing these would decrease the time before the method gave up and returned and so decrease the time spent using so much CPU, but might affect connecting - after all, the timeouts are there for a reason. This app can be used in odd connection situations, such as on board ship, where connecting could potentially be over satellite with high latency and take longer than a standard desktop internet connection. I do not know what reasonable timeouts would be. Also, there has to be a better method to avoid the CPU usage and terminate more quickly than simply shortening the timeouts.]

推荐答案

您可以在异步模式中使用WinInet。不确定是否可以解决CPU使用率问题,但是终止处理是否正确。

You can use WinInet in asynchronous mode. Not sure whether it solves CPU utilization issue but termination can be handled properly.

这篇关于在InternetOpenURL尝试(和失败)连接时,如何避免完全利用CPU或终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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