locked
Timer within Windows Service not working RRS feed

  • Question

  • Hi world,

    I have been working on a very simple windows service, that has a timer and every time it ticks it is supossed to add a line to a text file.

    After I installed the service and I run it, It adds a line in the onStart ()and in the onStop() methods, but it doesn't do anything when it ticks.

    Please, could somebody help me?

    Thanks  !!!!

     

    Imports System.ServiceProcess

    Imports System.Diagnostics

    Imports System.IO

    Public Class AutoRebootService2

    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.Interval = 1000

    Timer1.Enabled = True

    archivoLog("start", "c:/test.txt")

    End Sub

    Protected Overrides Sub OnStop()

    ' Add code here to perform any tear-down necessary to stop your service.

    Timer1.Enabled = False

    archivoLog("stop", "c:/test.txt")

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    archivoLog("10 secs", "c:/test.txt")

    End Sub

    Public Sub archivoLog(ByVal errorDescription As String, ByVal path As String)

    Dim objStreamWriter As StreamWriter

    objStreamWriter = File.AppendText(path)

    objStreamWriter.WriteLine(errorDescription)

    objStreamWriter.Close()

    End Sub

    End Class

    Monday, July 17, 2006 9:55 PM

Answers

  • It is considered invalid to show a form from a Service.  There are several reasons, one being that there may not be a user logged on and no desktop to show the form on.  Security and permissions can become another issue.

    Normally you would have a service and a seperate application that uses a ServiceController component to communicate with the service.

    All that being said, this may not be the best solution for your problem.  If you're going to want the UI anyway, you can probably just use the Windows Task Scheduler to run your app on a regular schedule.  You can find it in Start -> All Programs -> Accessories -> System Tools -> Task Scheduler.

    -EDIT-

    Forgot to mention that the Task Scheduler is accessible via the AT command so if you want, you can have your application create the scheduled task itself.  Just use System.Diagnostics.Start() to run the AT command with the appropriate arguments.

    Tuesday, July 18, 2006 12:16 AM
    Moderator
  • I also had a problem finding this one. Turns out that for searching ... "Setup Windows Service" are the right keywords. The following article solves the problem...

    ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.KB.v10.en/enu_kbvcsharpnetkb/vcsharpnetkb/816169.htm

    Specifically the following part...

    1. For correct installation, add only primary output. To add the custom actions, follow these steps:
    2. In Solution Explorer, right-click ServiceSetup, point to View, and then click Custom Actions
    3. Right-click Custom Actions, and then click Add Custom Action.
    4. Click Application Folder, and then click OK.
    5. Click Primary output from LogWriterService (Active), and then click OK.

      Notice that Primary output appears under Install, Commit, Rollback and Uninstall.

    Hope this helps you. Took me an hour to find it. There is just too much information...

     

    Saturday, December 16, 2006 8:10 AM

All replies

  • Does the service stay running once you start it?  Or does it start and then stop?  Normally you would start a long running process of some sort - often a loop.  I'm not sure whether the Timer's thread counts or not...

    Monday, July 17, 2006 10:03 PM
    Moderator
  • Hi the service starts and stays running, and like I said when I stop it after some minutes, I check my text file and it contains only to lines:

    start

    stop

    But I don't know why it doesn't do anything when the timer ticks.

    I need to deploy an application to 40 computers and it needs to be run  at 2:00am every night, and I though that having a windows service checking the time would be the solution but It has been way painful since I started with this

    Do you have any suggestions

    Thank you so much for you reply.

     

    Monday, July 17, 2006 10:11 PM
  • Hi world, I have changed my code so that the windows service won't be in charge of checking the computer time, instead I will have a form, actually I need this user interface.

     

    Anyway in my OnStart method I try to show the form, but the form shows and then freezes, and it will be labeled as:

     

    frmReboot is not responding....

     

    Protected Overrides Sub OnStart(ByVal args() As String)

         Dim SecondForm As New frmReboot

        SecondForm.Show()

    End Sub

     

    Can somebody help me please?

    Monday, July 17, 2006 10:54 PM
  • It is considered invalid to show a form from a Service.  There are several reasons, one being that there may not be a user logged on and no desktop to show the form on.  Security and permissions can become another issue.

    Normally you would have a service and a seperate application that uses a ServiceController component to communicate with the service.

    All that being said, this may not be the best solution for your problem.  If you're going to want the UI anyway, you can probably just use the Windows Task Scheduler to run your app on a regular schedule.  You can find it in Start -> All Programs -> Accessories -> System Tools -> Task Scheduler.

    -EDIT-

    Forgot to mention that the Task Scheduler is accessible via the AT command so if you want, you can have your application create the scheduled task itself.  Just use System.Diagnostics.Start() to run the AT command with the appropriate arguments.

    Tuesday, July 18, 2006 12:16 AM
    Moderator
  • thank you so much for your reply rkimble,

    But as far as I concern you have to specify an account in order to run a schedule task right?

    I wouldn't like to hard code the user name and not mention the password.

    Can I use the localsytem account?

    I will do some research about  the ServiceController component. do you suggest any information source?

    One more thing, this is even more important, how do you deploy a windows service?

    How do you run :

    InstallUtil "c:\path\myservice"

    from the setup project ?

     

    THANKS !!!!

    Tuesday, July 18, 2006 6:02 AM
  • I also had a problem finding this one. Turns out that for searching ... "Setup Windows Service" are the right keywords. The following article solves the problem...

    ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.KB.v10.en/enu_kbvcsharpnetkb/vcsharpnetkb/816169.htm

    Specifically the following part...

    1. For correct installation, add only primary output. To add the custom actions, follow these steps:
    2. In Solution Explorer, right-click ServiceSetup, point to View, and then click Custom Actions
    3. Right-click Custom Actions, and then click Add Custom Action.
    4. Click Application Folder, and then click OK.
    5. Click Primary output from LogWriterService (Active), and then click OK.

      Notice that Primary output appears under Install, Commit, Rollback and Uninstall.

    Hope this helps you. Took me an hour to find it. There is just too much information...

     

    Saturday, December 16, 2006 8:10 AM