Cancellation

Every method that sends a request has an Async form that takes a CancellationToken: client operations such as client.Users.FindAsync, object methods such as user.UpdateAsync and SaveAsync, the list methods LoadNextPageAsync, AllAsync and ListAutoPaging(cancellationToken), and the file transfers client.RemoteFiles.UploadFileAsync and DownloadFileAsync.

Cancelling the token stops the operation wherever it is: waiting for a response, waiting to retry, reading or writing file contents, or between pages and upload parts. The operation then ends with an OperationCanceledException (or a TaskCanceledException, which derives from it), and it starts no further request, retry, page or part.

The methods without a token, such as User.Find and LoadNextPage, work as before and cannot be cancelled.

Example Request

using FilesCom;
using FilesCom.Models;

using (var cancellation = new CancellationTokenSource(TimeSpan.FromMinutes(10)))
{
    try
    {
        await client.RemoteFiles.UploadFileAsync(localPath, destinationPath, cancellationToken: cancellation.Token);
    }
    catch (OperationCanceledException)
    {
        Console.WriteLine("The upload was cancelled.");
    }
}

What Cancellation Leaves Behind

Cancellation stops the SDK; it does not undo requests Files.com has already received. If the last request of an upload was sent before you cancelled, the file may still be completed.

The SDK passes the token to the streams you give it. A stream that ignores its token finishes its current read or write before the operation stops.

  • UploadFileAsync(destinationPath, stream, ...) disposes the stream when it ends, including when it is cancelled.
  • DownloadFileAsync(path, stream) leaves your stream open.
  • DownloadFileAsync(path, localPath) closes the local file when it ends. After a failure or cancellation, the file may be partly written.

The ReadTimeout setting limits only the wait for a download's response headers. To limit a whole operation, use a token that cancels itself, as in the example.