0 votes
by (390 points)

If I have multiple call of Sftp.PutFileAsync, is it possible to cancel only selected upload? Does async variants accepts CancellationToken?

Applies to: Rebex SFTP

1 Answer

0 votes
by (58.9k points)
edited by
 
Best answer

Sorry, we do not currently offer async method variants that would accept the CancellationToken.

However, there is the Sftp.AbortTransfer(object state) accepting object state (that you can also feed to the Sftp client async methods). Based on this you can filter the async operations to be aborted.

UPDATE:
Since release 2015R4 value types can also be used as "state".
Please note that only object types can currently be used as "state" (value types are not supported yet).

Here is a short code example showing how to use the state to abort selected async transfers:

Sftp sftp = new Sftp();
sftp.Connect("server");
sftp.Login("user", "password");

Task[] tasks = new Task[10];

string abort = "abort";
string notAbort = "notAbort";

for (int i = 0; i < 10; i++)
{
    string state = i.ToString();

if (i == 0 || i == 3)
        tasks[i] = sftp.PutFileAsync(string.Format(@"C:\temp\file{0}.txt", i), string.Format("file{0}.txt", i), abort);
else
    tasks[i] = sftp.PutFileAsync(string.Format(@"C:\temp\file{0}.txt", i), string.Format("file{0}.txt", i), notAbort);
}

// abort transfers 0 and 3
sftp.AbortTransfer(abort);

Task.WaitAll(tasks);
by (390 points)
Alright, that would be sufficient for my needs :D
Thanks for the help as usual
...