0 votes
by (260 points)
edited

I have the following code which starts a download:

_transferClient = new Ftp { Timeout = -1 };
_transferClient.Options |= FtpOptions.KeepAliveDuringTransfer;
_transferClient.Connect(SessionSettings.Server);
_transferClient.Login(SessionSettings.Username, SessionSettings.Password);
_transferClient.GetFile(RemotePath, LocalPath);

Then, in a button event handler, I have this code which attempts to cancel the upload, disconnect, then dispose the Ftp object

_transferClient.Abort();
_transferClient.Disconnect();
_transferClient.Dispose();
_transferClient = null;

What is the correct way to do this without receiving the "Another operation is pending" exception?

Applies to: Rebex FTP/SSL

1 Answer

0 votes
by (132k points)
edited
 
Best answer

If you simply need to abort the transfer and disconnect, just call the Dispose method (without calling Abort or Disconnect first). This will immediately close both the data connection and control connection and won't throw "Another operation is pending" exception.

The Abort method is only useful if you need to abort the transfer but keep the connection active. It doesn't abort the transfer immediately, but within several seconds. This means you would have to wait for the actual operation to fail before calling another method (including Disconnect).

by (260 points)
edited

thanks a lot. that seems to work fine

...