Как читать файл одновременно из нескольких потоков

using (var stream = File.Open(@"./Files/HelloSticker.tgs", FileMode.Open))
{
    InputOnlineFile iof = new InputOnlineFile(stream);
    iof.FileName = "StickerHello.tgs";
                
    await client.SendDocumentAsync(userId, iof, replyMarkup: new ReplyKeyboardMarkup(new MainKeyboard().OpenMainKeyboard(), true), cancellationToken: cancellationToken);
}

Написал такой код, но возникает следующая ошибка

System.IO.IOException: The process cannot access the file because it is being used by another process.

Метод SendDocumentAsync из бибилиотеки Telegram.Bot


Ответы (1 шт):

Автор решения: aepot

Попробуйте исправить открытие файла так, чтобы при открытии разрешался доступ на чтение другим потокам - FileShare.Read.

var stream = File.Open(@"./Files/HelloSticker.tgs", FileMode.Open, FileAccess.Read, FileShare.Read)
→ Ссылка