session.SynchronizeDirectories can not be stopped if its started?

Advertisement

cyo
Joined:
Posts:
1
Location:
China

session.SynchronizeDirectories can not be stopped if its started?

I use Session.SynchronizeDirectories to download 1GB data from remote server via FTP. Once its started, by CancellationToken, I CANNOT stop it.

Can anyone check my code as below:
private CancellationTokenSource cancellationTokenSource;
 
public async Task Download(
    Session session, string TargetIP, string TargetPath, string DestiFolder,
    CancellationToken cancellationToken)
{
    Stopwatch sw = Stopwatch.StartNew();    
 
    try
    {
        // Setup session options
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Ftp,
            HostName = TargetIP,
            UserName = "u1",
            Password = "p1",
            FtpMode = FtpMode.Passive,
            TimeoutInMilliseconds = 30000
        };
 
        session.FileTransferProgress += (sender, e) => SessionFileTransferProgress(sender, e, TargetIP);
        session.Open(sessionOptions);
 
        cancellationToken.ThrowIfCancellationRequested();
 
        TransferOptions transferOptions = new TransferOptions();
        transferOptions.TransferMode = TransferMode.Binary;
        transferOptions.ResumeSupport.State = TransferResumeSupportState.On;
 
        SynchronizationResult synchronizationResult;
        synchronizationResult =
            session.SynchronizeDirectories(
                SynchronizationMode.Local, DestiFolder, TargetPath, false);
 
        cancellationToken.ThrowIfCancellationRequested();
 
        synchronizationResult.Check();
 
        sw.Stop();
 
        session.Close();
        return;
    }
    catch (OperationCanceledException)
    {
        session.Close();                    
        session.Dispose();
        session.Abort();
        throw;
    }
    catch (Exception e)
    {
        Debug.WriteLine($"Error: {e}");
        session.Dispose();
    }
}
 
private void button11_Click(object sender, EventArgs e)
{
    cancellationTokenSource.Cancel();
}
The issue is once downloading is started, it can not be stopped. I can only close my C# application to stop it.

Can anyone help? Thanks

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum