Solved

Timer does not work for My Wndow Service

Posted on 2004-09-21
1
243 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
1 Comment
 
LVL 85

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now