Home > front end >  Weird behavior with OnShutdown() event in a Windows Service
Weird behavior with OnShutdown() event in a Windows Service

Time:02-22

I have developed a windows service in vb.net (VS2017, target .net framework 4.5).
The windows service do automatically sql-server backups (timer based), delete old backup files, read and write a .ini file and send emails in some situations and - of curse - write to the event log. The Service has to run on a windows server 2019 (standard).
Anything works as expected - except the handling of the OnShutdown() event, that drives me crazy, as it is a "moving target".
If the server is shut down (normal case should be "restart"), I want to:

  • log in the event log
  • write in a .ini
  • send an email

My code zu OnShutdown():

Protected Overrides Sub OnShutdown()
  cModul = "OnShutdown()"
  ProtokollSchreiben("Der Server wird heruntergefahren...", Typ_Warning)
 '
  DebugProtokollSchreiben("Timer stoppen und anhalten...", Typ_Warning)
  MyTimer.Stop()
  MyTimer.Enabled = False
  DebugProtokollSchreiben("Timer gestoppt und angehalten...", Typ_Warning)
  DebugProtokollSchreiben("Der Server wird heruntergefahren...", Typ_Warning)
  DebugProtokollSchreiben("Vor schreiben .ini...", Typ_Warning)
  cIniFile.WriteInteger("WindowsService", "ServerWurdeGebooted", 1)
  DebugProtokollSchreiben("Nach schreiben .ini...", Typ_Warning)
  DebugProtokollSchreiben("Vor MailversandServerReboot...", Typ_Warning)
  MailversandServerReboot()
 cModul = "OnShutdown()"
  DebugProtokollSchreiben("Nach MailversandServerReboot...", Typ_Warning)
 '
   MyBase.OnShutdown()' call “original event”
End Sub

Notes:
I set:

Me.CanShutdown = True

in the designer code and write the actual value at startup in the event log (CanShutdown is set to true at runtime).
In ProtokollSchreiben() and DebugProtokollSchreiben() only log entries are written. In MailversandServerReboot() an email is sent over SMTP (this works in other parts of the service without problems).

Behavior:

  • sometimes the event (OnShutdown() is called, sometimes not
  • if it is called, mostly, the last event in the event log is "Vor schreiben .ini..." (= before write in the .ini file)
  • -> whereby the .ini in this cases sometimes is updated (although the next log entry "Nach schreiben .ini..." is not written to the event log), sometimes not

I have searched the internet and read this SO-Links:
Screenshot to flag files The "root directory" is \prog\ ( 1 ). In the sub directory \SaveRebootFlags\ ( 2 ) one flag file remains stored for every reboot.
Any file ( 3 ) contains a text with the timestamp to the server shutdown (not to the restart of the windows service) In the "root directory" \prog\ ( 1 ) a similar flag file is written,
but without the datetime stamp (only ( 4 ))... so this filename is always the same.
In OnStart() of the service, I check, if a flag file is stored in the "root directory" \Prog\ ( 1 ).
If yes, an email is sent "The server was down at 'timestamp in flag file', the windows service was restarted and works again...".
After sending the email, the "main flag file" is deleted automatically.
Note: As we have the files also in ( 2 ) we can comprehend any reboot.
This is not what I initially wanted, but it works (at least for me;-).
Maybe this helps somebody, that also is "stumbled" over a weird behavior...

  • Related