设置InternetReadFile的超时(WinInet + Delphi) [英] Set timeout for InternetReadFile (WinInet + Delphi)

查看:367
本文介绍了设置InternetReadFile的超时(WinInet + Delphi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要通过检索其文本内容来处理URL列表。我的互联网连接开始于

My application needs to process a list of urls, by retrieving their text content. My internet connection is started by

myTimeOut:= 2000;
InternetSetOption(nil, INTERNET_OPTION_CONNECT_TIMEOUT, Pointer(@myTimeOut), SizeOf(myTimeOut));
InternetSetOption(nil, INTERNET_OPTION_SEND_TIMEOUT, Pointer(@myTimeOut), SizeOf(myTimeOut));
InternetSetOption(nil, INTERNET_OPTION_RECEIVE_TIMEOUT, Pointer(@myTimeOut), SizeOf(myTimeOut));

我正在为每个URL阅读启动单独的线程

I'm launching separate threads for each url reading

(...)
read_url_threads[thread_counter]:= TReadURLThread.Create(false);
read_url_threads[thread_counter].FreeOnTerminate:= False;
read_url_threads[thread_counter].myURL:= target_url_list[i];
read_url_threads[thread_counter].output_filename:= local_output_filename;
read_url_threads[thread_counter].NetHandle:= NetHandle;
read_url_threads[thread_counter].limit_text_size:= max_length_url;
hArr[thread_counter]:= read_url_threads[thread_counter].Handle;
(...)
if (thread_counter >= max_threads) or
   (thread_counter >= (target_url_list.Count)) then

   repeat
     rWait:= WaitForMultipleObjects(thread_counter, @hArr,True, 100);
     Application.ProcessMessages;
   until rWait <> WAIT_TIMEOUT;
(...)

在线程执行过程中,我通过以下方式检索url内容: / p>

Inside the thread execution I retrieve the url content by:

   if Assigned(NetHandle) and (not EndsText('.pdf',url)) then
    try
      UrlHandle := InternetOpenUrl(NetHandle, PChar(url), nil, 0, 0, 0);
      if Assigned(UrlHandle) then
        try
          repeat
            InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
            SetString(StrBuffer, PAnsiChar(@Buffer[0]), BytesRead);
            Result := Result + StrBuffer;
          until BytesRead = 0;
        finally
          InternetCloseHandle(UrlHandle);
        end;

我的问题是为InternetReadFile例程设置超时。使用某些网址有时会持续整整几分钟。在此周期中强制超时的最佳方法是什么?

My problem is setting the timeout for the InternetReadFile routine. With some urls it gets stuck sometimes for whole minutes. What would be the best way to force a timeout in this cycle?

推荐答案

如果配置的超时无效(这是一个已知错误),那么您应该关闭连接句柄,这将迫使关联的函数失败,从而将控制权返回给您的程序。 Microsoft文章 Q224318 ,如何控制连接超时值通过创建第二个线程,描述了如何做到这一点。 (您需要一个单独的线程,因为另一个线程在等待 InternetReadFile 返回)。

If the configured timeout has no effect (which is a known bug), then you should close the connection handle, which will force the associated function to fail, thereby returning control to your program. Microsoft article Q224318, "How to control connection timeout value by creating second thread," describes how to do that. (You need a separate thread because the other thread is stuck waiting for InternetReadFile to return.)

这篇关于设置InternetReadFile的超时(WinInet + Delphi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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