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
  1. Home
  2. General Programming
  3. Visual Basic
  4. Window Service doesn't run continously using vb 2005

Window Service doesn't run continously using vb 2005

Scheduled Pinned Locked Moved Visual Basic
testingbeta-testingquestion
7 Posts 2 Posters 0 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.
  • C Offline
    C Offline
    CARisk3
    wrote on last edited by
    #1

    I'm not sure this is the correct place to post but since I wrote the code in vb I figured I would start here. I have a service that I created that writes to a text file, but it only writes 16 times and then stops. I have not asked it to stop it just does it on it's own. I am using this for testing because I have a bigger project that I need to create as a Window service and the timer wasn't working in it. So I created this small testing project and still can't get the timer to work correctly. Could some one please give me some clues as to what might be wrong with the code? I copied below what I am using. Public Class AuthorLog 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. Const iTIME_INTERVAL As Integer = 15000 ' 15 seconds. Dim oTimer As System.Threading.Timer Dim strline = vbCrLf System.IO.File.AppendAllText("C:\AuthorLog.txt", "AuthorLogService has been started at " & Now.ToString() & vbCrLf) Dim tDelegate As Threading.TimerCallback = AddressOf EventAction oTimer = New System.Threading.Timer(tDelegate, Me, 0, iTIME_INTERVAL) End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. End Sub Public Sub EventAction(ByVal sender As Object) System.IO.File.AppendAllText("C:\AuthorLog.txt", "AuthorLogService fires EventAction at " & Now.ToString() & vbCrLf) End Sub End Class Thanks for your assistance in advance. Carolyn

    If you can’t have fun at work, then why go to work?

    D 1 Reply Last reply
    0
    • C CARisk3

      I'm not sure this is the correct place to post but since I wrote the code in vb I figured I would start here. I have a service that I created that writes to a text file, but it only writes 16 times and then stops. I have not asked it to stop it just does it on it's own. I am using this for testing because I have a bigger project that I need to create as a Window service and the timer wasn't working in it. So I created this small testing project and still can't get the timer to work correctly. Could some one please give me some clues as to what might be wrong with the code? I copied below what I am using. Public Class AuthorLog 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. Const iTIME_INTERVAL As Integer = 15000 ' 15 seconds. Dim oTimer As System.Threading.Timer Dim strline = vbCrLf System.IO.File.AppendAllText("C:\AuthorLog.txt", "AuthorLogService has been started at " & Now.ToString() & vbCrLf) Dim tDelegate As Threading.TimerCallback = AddressOf EventAction oTimer = New System.Threading.Timer(tDelegate, Me, 0, iTIME_INTERVAL) End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. End Sub Public Sub EventAction(ByVal sender As Object) System.IO.File.AppendAllText("C:\AuthorLog.txt", "AuthorLogService fires EventAction at " & Now.ToString() & vbCrLf) End Sub End Class Thanks for your assistance in advance. Carolyn

      If you can’t have fun at work, then why go to work?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      It probably stops when the oTimer object get's garbage collected, since it's no longer in scope when your OnStart method gets to End Sub. Using a Timer to run a service is pretty much bad practice. You normally use the OnStart method to setup and start a new Thread containing your service code.

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

      C 1 Reply Last reply
      0
      • D Dave Kreskowiak

        It probably stops when the oTimer object get's garbage collected, since it's no longer in scope when your OnStart method gets to End Sub. Using a Timer to run a service is pretty much bad practice. You normally use the OnStart method to setup and start a new Thread containing your service code.

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

        C Offline
        C Offline
        CARisk3
        wrote on last edited by
        #3

        I am really not understanding the reply. I thought timers were used in services to set how often it should run. Could you explain how a service would be set up? Everything I have researched and looked up on services shows the setup like I have in the code snippet above. Thank you.

        If you can’t have fun at work, then why go to work?

        D 1 Reply Last reply
        0
        • C CARisk3

          I am really not understanding the reply. I thought timers were used in services to set how often it should run. Could you explain how a service would be set up? Everything I have researched and looked up on services shows the setup like I have in the code snippet above. Thank you.

          If you can’t have fun at work, then why go to work?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          CARisk3 wrote:

          I thought timers were used in services to set how often it should run

          Wrong. Timers are used in any application to execute code at a specific interval. They are not specific to services, and frankly, when I see a timer in a service, I immediately suspect it's not being used properly, or for the right reasons.

          CARisk3 wrote:

          I am really not understanding the reply.

          What's hard to understand? You simply stop the timer at the start of your timer tick handler and restart it at the end. That way, the timer won't call your code again until you're ready for it to do so.

          CARisk3 wrote:

          Everything I have researched and looked up on services shows the setup like I have in the code snippet above.

          That's the problem with using other peoples code. You don't know why they're doing something, or if it's even the correct way to do want you want.

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

          C 1 Reply Last reply
          0
          • D Dave Kreskowiak

            CARisk3 wrote:

            I thought timers were used in services to set how often it should run

            Wrong. Timers are used in any application to execute code at a specific interval. They are not specific to services, and frankly, when I see a timer in a service, I immediately suspect it's not being used properly, or for the right reasons.

            CARisk3 wrote:

            I am really not understanding the reply.

            What's hard to understand? You simply stop the timer at the start of your timer tick handler and restart it at the end. That way, the timer won't call your code again until you're ready for it to do so.

            CARisk3 wrote:

            Everything I have researched and looked up on services shows the setup like I have in the code snippet above.

            That's the problem with using other peoples code. You don't know why they're doing something, or if it's even the correct way to do want you want.

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

            C Offline
            C Offline
            CARisk3
            wrote on last edited by
            #5

            Could you help me understand? I need to have a program run every half hour to check the server to see if new folders have been created. If new folders have been added then I need to update the database with the new folder names. (The reason I am doing this is we have a content management system and if the users add new folders to the server then the cms doesn't recognize them. The users are putting content into the folders and then linking to files within the new folders from cms.) To my understanding this should be a service. I created the routine in Visual Studio 2005 and when I run it manually, it does what I need it to do. It adds the folders and permissions correctly. The problem is I don't want to have to remember to run the application every half hour. Plus our users could be adding content at any time of the day or night. I really need to understand how services work and how to set them up correctly as my supervisor is wanting me to automate some other routines. If you don't have time to explain, could you point me in the direction of a book or website with a good explanation and examples. I've been trying to get this to work for a couple of months.

            If you can’t have fun at work, then why go to work?

            D 1 Reply Last reply
            0
            • C CARisk3

              Could you help me understand? I need to have a program run every half hour to check the server to see if new folders have been created. If new folders have been added then I need to update the database with the new folder names. (The reason I am doing this is we have a content management system and if the users add new folders to the server then the cms doesn't recognize them. The users are putting content into the folders and then linking to files within the new folders from cms.) To my understanding this should be a service. I created the routine in Visual Studio 2005 and when I run it manually, it does what I need it to do. It adds the folders and permissions correctly. The problem is I don't want to have to remember to run the application every half hour. Plus our users could be adding content at any time of the day or night. I really need to understand how services work and how to set them up correctly as my supervisor is wanting me to automate some other routines. If you don't have time to explain, could you point me in the direction of a book or website with a good explanation and examples. I've been trying to get this to work for a couple of months.

              If you can’t have fun at work, then why go to work?

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Normally, you'd use the OnStart event of the server to start a new thread. This thread, in your case, would setup a FileSystemWatcher and sit there, waiting for events. The FileSystemWatcher fires a Created event whenever a file or folder is created in the directory it's watching. This is where you'd do you database update. You wouldn't use a Timer to do this at all. Try looking at this[^] and this[^].

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

              C 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Normally, you'd use the OnStart event of the server to start a new thread. This thread, in your case, would setup a FileSystemWatcher and sit there, waiting for events. The FileSystemWatcher fires a Created event whenever a file or folder is created in the directory it's watching. This is where you'd do you database update. You wouldn't use a Timer to do this at all. Try looking at this[^] and this[^].

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

                C Offline
                C Offline
                CARisk3
                wrote on last edited by
                #7

                Thank you very much. I didn't know about the file watch feature. I will read through the two links and see if I can apply them. Do you know of any good books or websites that explain how to set up services that aren't file watchers? We are also wanting to set up some services that will synchronize data between a bought system and our in house systems. Once again thank you very much for taking the time to respond to my questions and pointing me in the right direction. :)

                If you can’t have fun at work, then why go to work?

                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