?
Solved

Timer does not work for My Wndow Service

Posted on 2004-09-21
1
Medium Priority
?
260 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 80 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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