Solved

Timer does not work for My Wndow Service

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal Report for VS2013 17 71
System32Int Error 8 55
Get Top Visible Row of Datagridview 6 26
Help with AsEnumerable(), LINQ 4 19
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

948 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

19 Experts available now in Live!

Get 1:1 Help Now