What is the correct way to abort transfer then disconnect and dispose connection?

0 votes
asked Apr 20, 2011 by John Hancock (260 points)
edited Apr 22, 2011

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
answered Apr 20, 2011 by Lukas Pokorny (116,670 points)
edited Apr 21, 2011
 
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).

commented Apr 21, 2011 by John Hancock (260 points)
edited Apr 21, 2011

thanks a lot. that seems to work fine

...