Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
3.3k views
in .NET FTP by (580 points)
I need some help getting the CancelTransfer method to work.

My GUI thread starts a new thread to do the actual transfer, which is wrapped in a class.

The method that starts the transfer contains:

public void BeginUpload()
{
    ftpConn = new FTPConnection();
    ftpConn.Connect();          
    ftpConn.UploadFile(_fileToUpload, _remoteFileName, false);
}


And the main GUI thread starts the Upload thread like so:

threadFTP = new System.Threading.Thread(ftpThread.BeginUpload);
threadFTP.Name = "FTPThread";
threadFTP.Start();

while (!threadFTP.Join(500))
{
    if (this.bCancelled)
    {
        ftpThread.CancelUpload();
    }
    Application.DoEvents();
}

lblStatus3.Text = ""; //Line after thread.Join


All of the events work fine (BytesTransferred, Uploaded, Closed, etc) in the case of a completed transfer. the bCancelled variable is set when a user clicks the Cancel button on the form. I see that my class CancelUpload() method is called, but the application hangs on the ftpConn.Close() method. The thread.Join loop never ends and the application stops responding.

I call ftpConn.Close() rather than ftpConn.CancelTransfer because of the hints in the comments that Close() is preferred and will quit more gracefully:

[CancelTransfer] should not be used unless absolutely necessary. The server is not notified.


Sending true for abrubtClose doesn't help. What is the right way to cancel a transfer and get notified of that fact in the Uploaded event through e.Cancel?

If I call CancelTransfer() instead of Close(), the application does not hang and the Uploaded event fires immediately afterwards (with 200 MB left to go on the file being uploaded), but e.Cancel is false. Shouldn't it be true to indicate that the transfer was cancelled?

Here is the CancelUpload() method in the class that is running the separate upload thread:

public void CancelUpload()
{
    if (ftpConn != null && ftpConn.IsConnected)
    {
        ftpConn.Close();
    }
}

3 Answers

0 votes
by (51.6k points)
We have recently found some problems with the cancellation of transfers and have made some changes, though I think more will be necessary. If you write to support I will send you the latest source so that you can try it out to see if it's better.

- Hans (EDT)
0 votes
by (440 points)
I'm bumping this because I am having the exact same problem. Every time I call .Close() my application hangs.
Any updates on this problem?
0 votes
by (162k points)
Are you calling Close() to cancel the transfer? You should use CancelTransfer(). Close() is unlikely to work.

I'm bumping this because I am having the exact same problem. Every time I call .Close() my application hangs.
Any updates on this problem?

Categories

...