Solved

Timer does not work for My Wndow Service

Posted on 2004-09-21
1
248 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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