Hello!
I am developing a telegram bot using .NET and I am having an issue.
I have two timers that execute functions when elapsed.
- First timer re-posts a message (deletes a message and then posts identical copy, this is a workaround for the fact that you can't edit messages that are older then 24 hours in telegram, this functionality keeps messages always fresh and editable).
- Second timer just edits the message if needed.
The methods that are called when these timers elapse are marked async
. This is how I attach them to elapsed event:
timer1.Elapsed += async (source, eventArgs) => { await Repost(); };
timer2.Elapsed += async (source, eventArgs) => { await Edit(); };
The problem happens when Elapsed
events of these timers happen at the same time.
Even tho I await
inside of the lambda, methods Repost
and Edit
seem to be executed at the same time in the different threads asynchronously: Repost()
deletes the message, and then before message copy is created Edit
is trying to edit the deleted message, and I get exception.
Clearly I misunderstand en effect of await
inside of the lambda and what is going on in general.
My ultimate goal is to call these async
methods Repost()
and Edit()
synchronously, so that there is no conflict.
Thanks you for your time, this is my first encounter with asynchronous programming and I will be glad to hear any explanations or advice on what am I doing or what I should do.
Thank you!