Stopping Delphi Indy threads without having to wait end Timeout

后端 未结 2 608
礼貌的吻别
礼貌的吻别 2021-01-01 04:48

It\'s the first time I work on a multi-threads application with Delphi so all is still fesh for me, but I read a lot.

My thread is simple, to be short I just use Ind

相关标签:
2条回答
  • 2021-01-01 05:34

    I've had a similar issue using Indy and webservices where the system we were talking too were unreliable at best ( I had to set read timeouts of 30 mins for some calls).

    Since the Rio Calls ( and your IdHTTP ) calls are blocking your cannot canel/abort them directly.

    You can however (if using different threads) disconnect the underlying socket which will cause an exception in the get/webcall.

    I've done a quick hack to test the idea using just TIdHTTP and it appears to work as it does with the Rio.

    In your main/controlling thread you need to call the IDHTTP.Disconnect, then catch the exception that will be raised in you thread. From looking at your code you are creating the IdHTTP in the Thread.Execute, you will have to move the declaration of that to the class's constructor to allow an Abort function on your thread class to simple disconnect the socket.

    I've used Delph 7 and INDY10 just incase thats differnet to your setup.

    Hope that helps

    BTW

    If anyone knows of a better/less agresive method I'd love to see that too.

    0 讨论(0)
  • 2021-01-01 05:44

    The class TidHTTP has an OnWork event. Use this event to cancel the data downloading or uploading. How to cancel with this event? Fire an Abort silent exception.

    This is a pseudo code:

    THTTPThread = class(TThread)
        private
            HTTPComponent: TidHTTP;
            procedure OnHTTPProgress(ASender: TObject; AWorkMode: TWorkMode;
                                         AWorkCount: Int64);
        published
            procedure execute();
    end;
    
    implementation
    procedure THTTPThread.execute;
    begin
        Self.HTTPComponent := TidHTTP.Create(nil);
        with HTTPComponent do
        begin
            OnWork := Self.OnHTTPProgress;
            Get('http://www.google.com');
        end;
    end;
    
    procedure THTTPThread.OnHTTPProgress(ASender: TObject; AWorkMode: TWorkMode;
      AWorkCount: Int64);
    begin
    
        if Self.Terminated then
            Abort;
    end;
    

    Take into account that the stages on which OnWork are called are random. The event could be called at the middle of the request, at the begging or even at the end. The code above will work as if you press the stop button on the browser.

    [UPDATE] Forget to mention that if then OnWork event is not called when the Thread is hanged out for a reply of the server, the use can use a timeout if the "last response" was received a specified milliseconds ago. Use GetTickCount for this.

    0 讨论(0)
提交回复
热议问题