c# - 'stream.ReadTimeout' threw an exception of type 'System.InvalidOperationException' sending photo to telegram bot -
i wrote below code sending photo bot, in stream, have 2 exceptions read , write , photo not send.
i think maybe reason error, couldn't fix it:
stream.readtimeoutthrew exception of type 'system.invalidoperationexception'
using (var stream = system.io.file.open("a.jpg", filemode.open)) { var filetosend = new filetosend("a.jpg", stream); task.run(() => bot.sendphotoasync(u.message.chat.id, filetosend).configureawait(false)); }
the reason exception dispose stream immediatly after starting task.
the using statement calls dispose on stream instance when execution leaves block. can either remove using statement or - if method async - may await call sendphotoasync(). there no reason use thread task.run():
using (var stream = system.io.file.open("a.jpg", filemode.open)) { var filetosend = new filetosend("a.jpg", stream); await bot.sendphotoasync(u.message.chat.id, filetosend).configureawait(false)); } the state-machine created compiler await call takes care finally block of using statement (where stream.dispose() called) executed after task returned sendphotoasync has completed.
Comments
Post a Comment