Question Installing a windows Service in .Net

a5m0d1

Member
Joined
Mar 14, 2010
Messages
15
Programming Experience
1-3
I have had some success making a windows service but I can’t seem to fix a simple issue

I can get the service to run through my code once, but I can’t get it to loop, well I can but then the service will not start correctly and just times out while the code is running.

How can I get the service to start and then loop, at the moment it just says starting service and the code is looping but the service wont start! Windows times it out after a few mins.

Thanks for any help u can offer me.


VB.NET:
Public Class WwService
    Dim sDate As Date
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        Timer1.Enabled = True
        Timer1.Interval = 10000 '10 secs
    End Sub
    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        Timer1.Enabled = False
    End Sub
    Sub WRun()
        sDate = DateValue(Now)
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\WwService.mdb"
        con.Open()
        sql = "SELECT * FROM Table1"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "WwServ")
        con.Close()
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow
        dsNewRow = ds.Tables("WwServ").NewRow()
        dsNewRow.Item("Test_Date") = sDate
        ds.Tables("WwServ").Rows.Add(dsNewRow)
        da.Update(ds, "WwServ")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        WRun()
    End Sub
End Class
 
Got it working

VB.NET:
Public Class EmptyService
    Inherits System.ServiceProcess.ServiceBase

    Private timerThread As System.Threading.Thread
    Private Timer1 As New System.Timers.Timer
    Dim sDate As Date
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Private Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.AutoLog = False ' We will write our log events ourself
        Me.CanPauseAndContinue = True
        AddHandler Timer1.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf TimerElapsed)

        Timer1.Interval = 3000

    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work
        Timer1.Enabled = True
        timerThread = New System.Threading.Thread(AddressOf Timer1.Start)
        timerThread.Start()

        'Add event log entry
        evlMain.WriteEntry("Empty Service Started.")

    End Sub

    Protected Overrides Sub OnPause()
        MyBase.OnPause()
        'Add event log entry
        evlMain.WriteEntry("Empty Service Paused.")
    End Sub

    Protected Overrides Sub OnContinue()
        MyBase.OnContinue()
        'Add event log entry
        evlMain.WriteEntry("Empty Service Continuing.")
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.

        Timer1.Enabled = False

        'Add event log entry
        evlMain.WriteEntry("Empty Service Stopped.")

    End Sub

    Private Sub TimerElapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
       
        ' This sub runs every time the elapsed milli-seconds of the timer pass
        ' Sp add your code here.
        sDate = DateValue(Now)
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\WwService.mdb"
        con.Open()
        Sql = "SELECT * FROM Table1"
        da = New OleDb.OleDbDataAdapter(Sql, con)
        da.Fill(ds, "WwServ")
        con.Close()
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow
        dsNewRow = ds.Tables("WwServ").NewRow()
        dsNewRow.Item("Test_Date") = sDate
        ds.Tables("WwServ").Rows.Add(dsNewRow)
        da.Update(ds, "WwServ")
    End Sub

End Class
 
Back
Top