Board index » delphi » detecting cancel after starting lengthy OLE automation call

detecting cancel after starting lengthy OLE automation call


Though there is no way that I am aware of to abort an OLE call (unless you
wrote the OLE server and it has a method that you wrote to abort the call).
It is possible to abort the loop. Just add an application.processmessages
to the bottom of the loop. If you do not, the onclick event of your abort
button will not be processed until after you exit the loop.

It would be something like...

   TYourForm = class(TForm)
        AbortButton : TButton;
        procedure AbortButtonOnClick(Sender);
        cancel_flag : boolean;
        procedure oleprocedure;

procedure TYourForm.oleprocedure;
var i : integer;
begin // the OLE loop
i := 1;
cancel_flag := false;
while (i < number_of_loops) and (not cancel_flag)
   do begin
      // your code here ...

procedure TYourForm.AbortButtonOnClick(Sender);
cancel_flag := true;

Hope that helps...

Kevin Flanagan


Re:detecting cancel after starting lengthy OLE automation call

You can use multithreading to accomplish this. Spawn a thread either in the
client or server and write some code that makes the thread reachable and
terminatable upon demand.

have fun
Binh Ly

Matt Robinson <> wrote in message
> I have a situation where I'm making OLE automation calls inside a loop.
> Each OLE automation call can take a while to finish (it's transferring
> files so it could be minutes or more).  When the user starts the process
> I display a status form with a cancel button.  I would like the user to
> be able to click the cancel button to abort what they started.  After I
> make the OLE automation call I don't get control back until the file
> transfer has completed.  When I try to use the cancel button my program
> does not detect that it's been clicked.  Here's some pseudo code:

> Application:= GetActiveOleObject('program');  // or CreateOleObject
> for iLoop:= 1 to iNumberOfFilesToTransfer do
> begin
>    ....
>    retVal:= Application.TransferFile(sFilename);
>    // once the above call is made the program doesn't get back control
> until the file transfer is done
>    WantToDetectCancelButtonHere;
>    // how can I detect a button press on my form while the file was
> being transferred?
>    ...
> end;

> I have two questions:

> 1:  Is there any general way to abort an ole automation call or is it
> completely dependent on the ole automation server?

> 2:  If I can't break out of an existing file transfer I would at least
> like to be able to not transfer any more files (If the user selects 5
> files to transfer and if they click cancel during the first file it
> would complete the transfer of the first file, but then stop before
> starting the other 4 transfers.).  So the question is if the user clicks
> on cancel during the first file transfer, how can I detect it?

> Thanks for any help you can offer.

> Matt Robinson

Other Threads