The only thing I see directly is that async / await was not fully implemented before the event handler, since "SendNewMessages" returns void. And your event handler is not asynchronous.
According to MSDN in the section "Async return types (C # and Visual Basic)"
The main use of the void return type (helper routines in Visual Basic) refers to event handlers where the void return type is required. The void return can also be used to override the void return methods or for methods that perform actions that can be classified as fire and forget.
This is most likely a problem in your scenario, so you can try changing your SendNewMessage to this
async static Task SendNewMessages()
And your event handler for this
private async static void OnTimedEvent(object source, ElapsedEventArgs e) { await SendNewMessages(); }
UPDATED
It would also be nice to add some error handling code to your SendNewMessages method, since if an exception is thrown, your service will close.
async static Task SendNewMessages() { try { ... Your code here } catch(Exception e) { ... exceptionhandling here } }
At the moment, you only have exception handling in your foreach, but you do not have error handling (as far as I can see) for your database code.
if an exception is thrown here
MyDataContext myDB = new MyDataContext(); var newMessages = myDB.TelegramMessages.Where(tm => tm.Status != "New Message"); foreach (TelegramMessage newMessage in newMessages)
or here:
newMessage.Status = ex.Message; myDB.SubmitChanges();
Service will end
source share