Restart the service when an exception is thrown

I am writing a Windows service that should work 24 hours a day. This is a fairly simple service that keeps track of the directory into which files are deleted and processes these files. I need to restart the service if an unhandled exception is thrown.

Is there a way to restart the service in case of an unhandled exception?

+3
source share
6 answers

The Services applet has many recovery features:

Services Recovery

It can take different actions during the first, second and subsequent failures:

  • Restart the service after a custom delay
  • Run the program (pass command line options, possibly including the number of crashes)
  • ( )

, , (, ), , .

, , , , .

, , , " " " ", .

+2

"" "" - , " " - " "

+2

, , . " CodeProject", /​​ . , .

http://www.codeproject.com/KB/install/sercviceinstallerext.aspx

  • DLL.

  • ProjectInstaller.Designer.vb
    InitializeComponent Sub
    CHANGE
    Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
    Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller

    Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
    Me.ServiceInstaller1 = New Verifide.ServiceUtils.ServiceInstallerEx

  • ProjectInstaller.Designer.vb
     CHANGE
     Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
     Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller
     
     Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
     Friend WithEvents ServiceInstaller1 As Verifide.ServiceUtils.ServiceInstallerEx


  •  Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})

     Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceInstaller1, Me.ServiceProcessInstaller1})

  • ProjectInstaller.vb

  • ProjectInstaller.vb Public Sub , Initialize
     ADD
    'Set Reset Time Count - This Is 4 Days Before Count Is Reset
    ServiceInstaller1.FailCountResetTime = 60 * 60 * 24 * 4
    'ServiceInstaller1.FailRebootMsg = "Houston! We have a problem"

    'Add Failure Actions
    ServiceInstaller1.FailureActions.Add(New FailureAction(RecoverAction.Restart, 60000))
    ServiceInstaller1.FailureActions.Add(New FailureAction(RecoverAction.Restart, 60000))
    ServiceInstaller1.FailureActions.Add(New FailureAction(RecoverAction.None, 3000))

    ServiceInstaller1.StartOnInstall = True

  • . Voila

+1

, .

0

, Try/Catch , .

, . ! , , , . , , , , . .

, . CPU/RAM, , , .

0

"John Saunders" "theGecko", , . Windows Service , , (, ), , , Service Protector. Windows 24x7.

!

0

Source: https://habr.com/ru/post/1713874/


All Articles