In this brief code I show you how Auto Restart your Application windows service
Open Microsoft Visual Studio and create new Service Project, identifies Service.vb and past this code
Imports System.IO Public Class clsReboot Private Const NAMEOFBAT = "bootI4Ms.bat" Private _nameservice As String Private _programData As String Private _pathService As String Sub New(programData As String, nameservice As String) _programData = programData _nameservice = nameservice End Sub Private Function createBat() As Boolean Dim bret As Boolean = False If DeleteBath() Then'delete exist file bat Try IO.File.AppendAllText(_programData & "\" & NAMEOFBAT, "net stop " & _nameservice & vbCrLf) IO.File.AppendAllText(_programData & "\" & NAMEOFBAT, "net start " & _nameservice & vbCrLf) bret=True Catch ex As Exception bret = False End Try Else dataLogger("Delete not Allowed", "createBat") End If Return bret End Function Private Function DeleteBath() As Boolean Dim bret As Boolean = True Dim prompt As String = "chkBat" Try If IO.File.Exists(_programData & "\" & NAMEOFBAT) Then IO.File.Delete(_programData & "\" & NAMEOFBAT) End If Catch ex As Exception bret = False dataLogger(ex.Message , prompt) End Try Return bret End Function Public Sub AutoRestartService() Dim prompt As String = "AutoRestartService" Try If createBat() Then Dim fileBath As String = _programData & "\" & NAMEOFBAT ' "bootI4Tk.bat" If Not System.IO.File.Exists(fileBath) Then dataLogger("Reboot not Allowed", prompt) Exit Sub End If dataLogger("Ok reboot", prompt) Dim procStartInfo As New ProcessStartInfo With procStartInfo .UseShellExecute = True .FileName = fileBath .WindowStyle = ProcessWindowStyle.Normal .CreateNoWindow = False .WindowStyle = ProcessWindowStyle.Hidden .UseShellExecute = True .Verb = "runas" 'add this to prompt for elevation End With Dim process As Process = process.Start(procStartInfo) Else dataLogger("Bath not found ", prompt) End If Catch ex As Exception dataLogger("Reboot error:" & ex.Message, prompt) End Try End Sub End Class 'TODO 'insert your desiderate path ro create a file bat and your service name to reboot Dim yourdesideratepath As string,yourservicename As string sub subReboot() Dim tryReboot As New clsReboot(yourdesideratepath,yourservicename) tryReboot=AutoRestartService() end sub 'this timer force Autorestart when day changed Private Sub Elapse(sender As Object, e As Timers.ElapsedEventArgs) Dim prompt As String = "Elapse" Dim t As Timers.Timer = TryCast(sender, Timers.Timer) t.Enabled = False Try If NewDay Now.Day Then 'day changed NewDay = Now.Day subReboot()'force restart End If Catch ex As Exception End Try t.Enabled = True End Sub Private Sub dataLogger(Byval txt,Byval prompt) 'TODO YOUR DESIDERATE CODE End Sub
you identifies OnStart Event and add
Protected Overrides Sub OnStart(ByVal args() As String) Dim dayP As New Timers.Timer AddHandler dayP.Elapsed, AddressOf Elapse dayP.Interval = 2000 dayP.Enabled = True End Sub
Compile project and install it
Running with Administrator Privileges cmd.exe and typing these row of command
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 c: installutil "C:\pathofyourservice\servicename.exe" pause exit
Your service is installed, from windows service find it and click on start,
wait until midnight to test it.
Advertisements
1 Pingback