wiz0u / WTelegramClient

Telegram Client API (MTProto) library written 100% in C# and .NET
https://wiz0u.github.io/WTelegramClient/
MIT License
956 stars 156 forks source link

How to abort a file transfer in the middle? #270

Closed JgBr123 closed 1 month ago

JgBr123 commented 1 month ago

I see that it was mentioned before that a file transfer could be aborted by throwing an exception in the progressCallback. The problem is that i couldn't manage to do that, as the exception is not passed to the UploadFileAsync method, and instead is thrown into my main thread.

wiz0u commented 1 month ago

yes, you also have to try..catch around your UploadFileAsync obviously

example with download:

var cts = new CancellationTokenSource(); // some other thread/task may trigger the cts.Cancel()
try
{
    using var fs = new FileStream("download.bin", FileMode.Create);
    await Client.DownloadFileAsync(document, fs, progress: (progress, total) => cts.Token.ThrowIfCancellationRequested());
}
catch (OperationCanceledException ex)
{
    Console.WriteLine("Transfer was aborted");
}