Solved

Timer does not work for My Wndow Service

Posted on 2004-09-21
1
254 Views
Last Modified: 2010-10-05
Hi There !
             I made a window servise. everything works fine except the timer does not work.
             all the functions called from onStart function works except the timer does not do its job.
             here is the code, Any suggestion will be much appericiated.

Imports System.ServiceProcess

Public Class Service1
    Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

    Public Sub New()
        MyBase.New()

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

        ' Add any initialization after the InitializeComponent() call

    End Sub

    'UserService overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' The main entry point for the process
    <MTAThread()> _
    Shared Sub Main()
        Dim ServicesToRun() As System.ServiceProcess.ServiceBase

        ' More than one NT Service may run within the same process. To add
        ' another service to this process, change the following line to
        ' create a second service object. For example,
        '
        '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
        '
        ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1}

        System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    ' NOTE: The following procedure is required by the Component Designer
    ' It can be modified using the Component Designer.  
    ' Do not modify it using the code editor.
    Public WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        '
        'Timer1
        '
        Me.Timer1.Enabled = CType(configurationAppSettings.GetValue("Timer1.Enabled", GetType(System.Boolean)), Boolean)
        Me.Timer1.Interval = CType(configurationAppSettings.GetValue("Timer1.Interval", GetType(System.Int32)), Integer)
        '
        'Service1
        '
        Me.AutoLog = CType(configurationAppSettings.GetValue("Service1.AutoLog", GetType(System.Boolean)), Boolean)
        Me.CanHandlePowerEvent = CType(configurationAppSettings.GetValue("Service1.CanHandlePowerEvent", GetType(System.Boolean)), Boolean)
        Me.CanPauseAndContinue = CType(configurationAppSettings.GetValue("Service1.CanPauseAndContinue", GetType(System.Boolean)), Boolean)
        Me.CanShutdown = CType(configurationAppSettings.GetValue("Service1.CanShutdown", GetType(System.Boolean)), Boolean)
        Me.CanStop = CType(configurationAppSettings.GetValue("Service1.CanStop", GetType(System.Boolean)), Boolean)
        Me.ServiceName = CType(configurationAppSettings.GetValue("Service1.ServiceName", GetType(System.String)), String)

    End Sub

#End Region

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

        Timer1.Enabled = True
        Timer1.Start()
        Timer1.InitializeLifetimeService()


    End Sub
    Sub WriteFirstTime()
        Dim a As New IO.FileStream("C:\a.htm", IO.FileMode.OpenOrCreate)
        Dim s As New IO.StreamWriter(a)
        s.WriteLine("this is a test  <br> ")
        s.Close()
    End Sub
    Protected Overrides Sub OnStop()

        Timer1.Enabled = False
        Timer1.Stop()

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim a As New IO.FileStream("C:\a.htm", IO.FileMode.Append)
        Dim s As New IO.StreamWriter(a)
        s.WriteLine("an other test  <br> ")
        s.Close()
        a.Close()

    End Sub
End Class



Thanks
0
Comment
Question by:hunjra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 20 total points
ID: 12117703
Remove the timer from your form and instead use one through code as shown below:

Imports System.ServiceProcess

Public Class Service1
    Inherits System.ServiceProcess.ServiceBase

' Windows Form Designer generated code

    Private myTimer As System.Timers.Timer    

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

        myTimer = New System.Timers.Timer(60000) ' One minute interval - change to your liking
        AddHandler myTimer.Elapsed, AddressOf Me.Elapsed
        myTimer.Start()
    End Sub

    Sub WriteFirstTime()
        Dim a As New IO.FileStream("C:\a.htm", IO.FileMode.OpenOrCreate)
        Dim s As New IO.StreamWriter(a)
        s.WriteLine("this is a test  <br> ")
        s.Close()
        a.Close()
    End Sub

    Protected Overrides Sub OnStop()
        myTimer.Stop()
    End Sub

    Private Sub Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs
        Dim a As New IO.FileStream("C:\a.htm", IO.FileMode.Append)
        Dim s As New IO.StreamWriter(a)
        s.WriteLine("another test  <br> ")
        s.Close()
        a.Close()
    End Sub

End Class
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question