Catastrophic error when canceling a MultipartFormData Post!!!

Giganews Newsgroups
Subject: Catastrophic error when canceling a MultipartFormData Post!!!
Posted by:  Diego BaƱuelos (dbanuel…@prodigy.net.mx)
Date: Tue, 26 Apr 2005

Hi!

I'm working with D7 and Indy 9.0.18 in creating an application that uploads
and downloads files to/from an http Server using Get and Post methods. Both
processes (uploads and downloads) runs in a separate thread that contains a
single TidHTTP component.

Everything works fine until here, but I also need a way to cancel the
upload/download operation.  Here is the problem: when I close the connection
when an upload is taking place, the program raises an exception:

Project raised an Exception class EidNotConnected with message "not
connected"

and after that, no other TidHttp Component in the same or other threads
works again with get or post methods. The application freezes, like if it
was on a deadlock or something!

This is the method for canceling the process, called in the main thread:

procedure TAttachmentThread.CancelWork(Sender: TObject);
begin
  DoCancel;
end;

procedure TAttachmentThread.DoCancel;
begin
  Terminate;

  if (httpClient.Connected) then
      httpClient.Disconnect;

// Notifies a main thread form that the process has been stopped..
  PostMessage (FFormHandle,WM_DOCANCEL,0,0);
end;

This is the upload operation:

procedure TAttachmentUploadThread.Execute;
var
  MPD : TIdMultipartFormDataStream;
begin
  MPD := TIdMultipartFormDataStream.Create;
  try
    try
      MPD.AddFormField(fldATTACHMENTID,InttoStr (FAttachmentId));
      MPD.AddFormField('SERVERFILENAME',Format
('att%d.dat',[FAttachmentId]));
      MPD.AddFile('ATTACHMENT',FFileName,'application/octet-stream');
      httpClient.Request.ContentType := MPD.RequestContentType;
      httpClient.Post(FServerName +
'/operations/uploadattachment.dbsp',MPD);
    except
      on E:Exception do
      begin
        if (E is EIdHTTPProtocolException) then
        begin
          FhttpError := (E as EIdHTTPProtocolException).ReplyErrorCode;
          if (not Terminated) then
            DoNotifyError(FhttpError);
        end;
      end;
    end;
  finally
    MPD.Free;
  end;
end;

What am I doing wrong? Do the exception needs to be propagated to the main
thread?

Thanks in advance

Diego Bañuelos

Replies