Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem with running a windows service in vb.net

Problem with running a windows service in vb.net

Scheduled Pinned Locked Moved Visual Basic
csharpsysadminjsonhelp
5 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #1

    I have a windows a service that periodically checks the tcp/ip connection of device by pinging it and returning the status. So for this i have used a timer.At first i tried to to dynamically create a timer using System.Timers.Timer and using the 'Elapsed'event.But the when i tried to start the service it wouldn't start. Then I used a timer from the toolbox and seemed to start fine,the code in onstop and onstart subs is executing.However the code in timer_tick event isn't.I don't know why.I start the timer in the onstart and stop it onstop. Below is code for the timer event.

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

        Dim api As New RfidApiLib.RfidApi
        Dim status1 As Integer
    
        Dim StrCon As String
        Dim conn As SqlConnection
        Dim host As String
        'Dim status1 As Integer
        test.WriteByDate("e:\\temp", "log", "service", "reader timer this connected") ' Writes to log file
        'mylog.WriteEntry("Connected", EventLogEntryType.Information)
    
        StrCon = "data source=XXX;initial catalog=XXX; uid=XXX; pwd=XXX;"
        conn = New SqlConnection(StrCon)
    
        conn.Open()
    
        Dim outadapter1 As New SqlDataAdapter("select \* from dbo.ReaderInfo where dbo.ReaderInfo.Reader\_location in (Select Reader\_location from dbo.Reader\_Status )", conn)
        Dim ds1 As New DataSet()
        outadapter1.Fill(ds1, "dbo.ReaderInfo")
    
        Dim outadapter2 As New SqlDataAdapter("Select \* from dbo.Reader\_Status where Remarks = 'C'", conn)
        Dim ds2 As New DataSet()
        outadapter1.Fill(ds2, "dbo.ReaderInfo")
    
        For i As Integer = 0 To ds2.Tables(0).Rows.Count - 1
    
            host = ds1.Tables(0).Rows(i)("ReaderIP").ToString
            Try
                'My.Computer.Network.Ping(host)
                Dim ping As New System.Net.NetworkInformation.Ping()
                Dim pingReply As System.Net.NetworkInformation.PingReply = ping.Send(host) 'ping ipaddress
    
                'test.WriteByDate("e:\\temp", "log", "service", "reader" & ds1.Tables(0).Rows(i)("Reader\_location").ToString & "disconnected" & e1.Message)
    
            Catch e1 As System.Net.NetworkInformation.PingException 'if not connected try reconnecting
    
                status1 = api.TcpConnectReader(ds1.Tables(0).Rows(i)("ReaderIP").ToString, ds1.Tables(0).Rows(i)("ReaderPort")) 'API call to connect to device
    
                If status1 = 0 Then ' successfully connected
    
    D P T 3 Replies Last reply
    0
    • S sohaib_a

      I have a windows a service that periodically checks the tcp/ip connection of device by pinging it and returning the status. So for this i have used a timer.At first i tried to to dynamically create a timer using System.Timers.Timer and using the 'Elapsed'event.But the when i tried to start the service it wouldn't start. Then I used a timer from the toolbox and seemed to start fine,the code in onstop and onstart subs is executing.However the code in timer_tick event isn't.I don't know why.I start the timer in the onstart and stop it onstop. Below is code for the timer event.

      Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

          Dim api As New RfidApiLib.RfidApi
          Dim status1 As Integer
      
          Dim StrCon As String
          Dim conn As SqlConnection
          Dim host As String
          'Dim status1 As Integer
          test.WriteByDate("e:\\temp", "log", "service", "reader timer this connected") ' Writes to log file
          'mylog.WriteEntry("Connected", EventLogEntryType.Information)
      
          StrCon = "data source=XXX;initial catalog=XXX; uid=XXX; pwd=XXX;"
          conn = New SqlConnection(StrCon)
      
          conn.Open()
      
          Dim outadapter1 As New SqlDataAdapter("select \* from dbo.ReaderInfo where dbo.ReaderInfo.Reader\_location in (Select Reader\_location from dbo.Reader\_Status )", conn)
          Dim ds1 As New DataSet()
          outadapter1.Fill(ds1, "dbo.ReaderInfo")
      
          Dim outadapter2 As New SqlDataAdapter("Select \* from dbo.Reader\_Status where Remarks = 'C'", conn)
          Dim ds2 As New DataSet()
          outadapter1.Fill(ds2, "dbo.ReaderInfo")
      
          For i As Integer = 0 To ds2.Tables(0).Rows.Count - 1
      
              host = ds1.Tables(0).Rows(i)("ReaderIP").ToString
              Try
                  'My.Computer.Network.Ping(host)
                  Dim ping As New System.Net.NetworkInformation.Ping()
                  Dim pingReply As System.Net.NetworkInformation.PingReply = ping.Send(host) 'ping ipaddress
      
                  'test.WriteByDate("e:\\temp", "log", "service", "reader" & ds1.Tables(0).Rows(i)("Reader\_location").ToString & "disconnected" & e1.Message)
      
              Catch e1 As System.Net.NetworkInformation.PingException 'if not connected try reconnecting
      
                  status1 = api.TcpConnectReader(ds1.Tables(0).Rows(i)("ReaderIP").ToString, ds1.Tables(0).Rows(i)("ReaderPort")) 'API call to connect to device
      
                  If status1 = 0 Then ' successfully connected
      
      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Your post isn't readable because it's too wide. Break the code sample lines into multiple lines to reduce the width of the post. From the limited information I got from your post (what I could read anyway), you used the Timer control in the ToolBox and dropped it on a Form?? This isn't going to work in a service because there is no message pump. In a service, you would normally use the System.Threading.Timer, which makes direct calls back to your code instead of using events.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      modified on Wednesday, January 7, 2009 2:20 PM

      1 Reply Last reply
      0
      • S sohaib_a

        I have a windows a service that periodically checks the tcp/ip connection of device by pinging it and returning the status. So for this i have used a timer.At first i tried to to dynamically create a timer using System.Timers.Timer and using the 'Elapsed'event.But the when i tried to start the service it wouldn't start. Then I used a timer from the toolbox and seemed to start fine,the code in onstop and onstart subs is executing.However the code in timer_tick event isn't.I don't know why.I start the timer in the onstart and stop it onstop. Below is code for the timer event.

        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

            Dim api As New RfidApiLib.RfidApi
            Dim status1 As Integer
        
            Dim StrCon As String
            Dim conn As SqlConnection
            Dim host As String
            'Dim status1 As Integer
            test.WriteByDate("e:\\temp", "log", "service", "reader timer this connected") ' Writes to log file
            'mylog.WriteEntry("Connected", EventLogEntryType.Information)
        
            StrCon = "data source=XXX;initial catalog=XXX; uid=XXX; pwd=XXX;"
            conn = New SqlConnection(StrCon)
        
            conn.Open()
        
            Dim outadapter1 As New SqlDataAdapter("select \* from dbo.ReaderInfo where dbo.ReaderInfo.Reader\_location in (Select Reader\_location from dbo.Reader\_Status )", conn)
            Dim ds1 As New DataSet()
            outadapter1.Fill(ds1, "dbo.ReaderInfo")
        
            Dim outadapter2 As New SqlDataAdapter("Select \* from dbo.Reader\_Status where Remarks = 'C'", conn)
            Dim ds2 As New DataSet()
            outadapter1.Fill(ds2, "dbo.ReaderInfo")
        
            For i As Integer = 0 To ds2.Tables(0).Rows.Count - 1
        
                host = ds1.Tables(0).Rows(i)("ReaderIP").ToString
                Try
                    'My.Computer.Network.Ping(host)
                    Dim ping As New System.Net.NetworkInformation.Ping()
                    Dim pingReply As System.Net.NetworkInformation.PingReply = ping.Send(host) 'ping ipaddress
        
                    'test.WriteByDate("e:\\temp", "log", "service", "reader" & ds1.Tables(0).Rows(i)("Reader\_location").ToString & "disconnected" & e1.Message)
        
                Catch e1 As System.Net.NetworkInformation.PingException 'if not connected try reconnecting
        
                    status1 = api.TcpConnectReader(ds1.Tables(0).Rows(i)("ReaderIP").ToString, ds1.Tables(0).Rows(i)("ReaderPort")) 'API call to connect to device
        
                    If status1 = 0 Then ' successfully connected
        
        P Offline
        P Offline
        programmervb netc
        wrote on last edited by
        #3

        Try using the Timer.Elapsed event instead and set the amount of time in the properties for the timer control.

        Humble Programmer

        1 Reply Last reply
        0
        • S sohaib_a

          I have a windows a service that periodically checks the tcp/ip connection of device by pinging it and returning the status. So for this i have used a timer.At first i tried to to dynamically create a timer using System.Timers.Timer and using the 'Elapsed'event.But the when i tried to start the service it wouldn't start. Then I used a timer from the toolbox and seemed to start fine,the code in onstop and onstart subs is executing.However the code in timer_tick event isn't.I don't know why.I start the timer in the onstart and stop it onstop. Below is code for the timer event.

          Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

              Dim api As New RfidApiLib.RfidApi
              Dim status1 As Integer
          
              Dim StrCon As String
              Dim conn As SqlConnection
              Dim host As String
              'Dim status1 As Integer
              test.WriteByDate("e:\\temp", "log", "service", "reader timer this connected") ' Writes to log file
              'mylog.WriteEntry("Connected", EventLogEntryType.Information)
          
              StrCon = "data source=XXX;initial catalog=XXX; uid=XXX; pwd=XXX;"
              conn = New SqlConnection(StrCon)
          
              conn.Open()
          
              Dim outadapter1 As New SqlDataAdapter("select \* from dbo.ReaderInfo where dbo.ReaderInfo.Reader\_location in (Select Reader\_location from dbo.Reader\_Status )", conn)
              Dim ds1 As New DataSet()
              outadapter1.Fill(ds1, "dbo.ReaderInfo")
          
              Dim outadapter2 As New SqlDataAdapter("Select \* from dbo.Reader\_Status where Remarks = 'C'", conn)
              Dim ds2 As New DataSet()
              outadapter1.Fill(ds2, "dbo.ReaderInfo")
          
              For i As Integer = 0 To ds2.Tables(0).Rows.Count - 1
          
                  host = ds1.Tables(0).Rows(i)("ReaderIP").ToString
                  Try
                      'My.Computer.Network.Ping(host)
                      Dim ping As New System.Net.NetworkInformation.Ping()
                      Dim pingReply As System.Net.NetworkInformation.PingReply = ping.Send(host) 'ping ipaddress
          
                      'test.WriteByDate("e:\\temp", "log", "service", "reader" & ds1.Tables(0).Rows(i)("Reader\_location").ToString & "disconnected" & e1.Message)
          
                  Catch e1 As System.Net.NetworkInformation.PingException 'if not connected try reconnecting
          
                      status1 = api.TcpConnectReader(ds1.Tables(0).Rows(i)("ReaderIP").ToString, ds1.Tables(0).Rows(i)("ReaderPort")) 'API call to connect to device
          
                      If status1 = 0 Then ' successfully connected
          
          T Offline
          T Offline
          Tim Carmichael
          wrote on last edited by
          #4

          The service I have has a timer (System.Timers.Timer) that works. Searching for help on this class should point you in the right direction. Tim

          S 1 Reply Last reply
          0
          • T Tim Carmichael

            The service I have has a timer (System.Timers.Timer) that works. Searching for help on this class should point you in the right direction. Tim

            S Offline
            S Offline
            sohaib_a
            wrote on last edited by
            #5

            ok it worked using the system.threading.timer class.Apparently the other two timers dont work in windows service in .net 2.0.read it online. I want to know more what this timer does.I searched online and what I figured is that it similar to the forms timer except that the timer event runs in a different thread using timercallback delegate. So is it something like a thread that runs periodically?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups