Problem with timer

Joined
Jun 6, 2008
Messages
9
Programming Experience
Beginner
Hello vb.net geniuses, i just want to ask some help with my code. Im trying to create a windows service program that writes a log every 30 minutes but i know how what the problem of my coding it wont works. :mad:

thanks in advance!


VB.NET:
Public Class Service1
    Private mvTimer As System.Timers.Timer
    Dim mLogFile As String

    Private Sub mvTimer_Elapsed(ByVal pSender As Object, ByVal pArgs As System.Timers.ElapsedEventArgs)
        'this event had better fire!!
        mvTimer.Enabled = False
        mvTimer.Stop()

        WriteLog("Hellow World!!!!")

        mvTimer.Enabled = True
        mvTimer.Start()
    End Sub


    Protected Overrides Sub OnStart(ByVal args() As String)
        mLogFile = My.Application.Info.DirectoryPath & "\WindowsService.Log"
        WriteLog("Service Starts now...")

        Dim TimerInterval As Integer = ((60000 * 60) * 30)
        mvTimer = New System.Timers.Timer(TimerInterval)
        AddHandler mvTimer.Elapsed, AddressOf mvTimer_Elapsed
        mvTimer.AutoReset = True
        mvTimer.Enabled = True
        mvTimer.Start()

    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        WriteLog("Service ends now...")

    End Sub

    Public Sub WriteLog(ByVal pMsg As String)
        pMsg = Format(Now, "HH:mm:ss : ") & pMsg & vbCrLf & vbCrLf
        FileIO.FileSystem.WriteAllText(mLogFile, pMsg, True)
    End Sub

End Class
 
Hello,
I did not test out this code, but its only some adjustments to how you were doing things. I think this will work where yours failed. I didnt test it becuase I didnt want to work with the log.

VB.NET:
Public Class Service1
    Friend WithEvents Not_mytimer As New System.Timers.Timer
    Dim mLogFile As String

    Protected Overrides Sub OnStart(ByVal args() As String)
        mLogFile = My.Application.Info.DirectoryPath & "\WindowsService.Log"
        WriteLog("Service Starts now...")

        Dim TimerInterval As Integer = ((60000 * 60) * 30)
        Not_mytimer.Interval = TimerInterval
        Not_mytimer.AutoReset = True
        Not_mytimer.Enabled = True
        Not_mytimer.Start()
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        WriteLog("Service ends now...")

    End Sub

    Public Sub WriteLog(ByVal pMsg As String)
        pMsg = Format(Now, "HH:mm:ss : ") & pMsg & vbCrLf & vbCrLf
        FileIO.FileSystem.WriteAllText(mLogFile, pMsg, True)
    End Sub

    Private Sub Not_mytimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Not_mytimer.Elapsed
        'this event had better fire!!
        Not_mytimer.Enabled = False
        Not_mytimer.Stop()

        WriteLog("Hellow World!!!!")

        Not_mytimer.Enabled = True
        Not_mytimer.Start()
    End Sub
End Class
 
Hello.

I have the exact same Problem.
I've already digged through everything what Google has on this topic, and I'm telling what I found:
The System.Windows.Forms.Timer is not working within a Service for some reason (some people say that it is related to the missing MessageLoop/Pump), it's suggested to use use Timers.Timer instead (like you do). Also there are topics which state that this timer is also not working, and oyu should use Threading.Timer, while most people say that that one is working.

However, it's not working for me. :( Not one of the three timers is working within my service...while the same code copied to a Form does work perfect. Here is what I have with Threading.Timer:

VB.NET:
    Private Timer As Threading.Timer
    Private TimerCallBack As New Threading.TimerCallback(AddressOf Me.Timer_Tick)

    Protected Overrides Sub OnStart(ByVal args() As String)
        Me.Timer = New Threading.Timer(Me.TimerCallBack, Nothing, 150, 4000)
    End Sub

    Private Sub Timer_Tick(ByVal state As Object)
        'do something...but it never fires...
    End Sub

Bobby
 
Hello Bobby,
My own experiance with the difference between forms and services is fairly limited. I did however learn much during this time. One of the things I learned was that forms/applications are very, very, very forgiving in what the code does when compared to what a service code does.

I agree with the others that the forms.timers are missing parts and is the reason why it does not work in the service. As for the threading.timer I have never worked with it and I do not plan on playing around with it yet. I do have serveral system.timers working in different projects.

I have looked at your code that you supplied and I must ask why you decided that a delegate/call back would work easily. Also, what version of .net are you using?
I put the following code together based on what you put down. I did not test it becuase I did not want to spend the time installing and then uninstalling.
VB.NET:
Public Class Service1
Private WithEvents Timer As System.Timers.Timer
    Protected Overrides Sub OnStart(ByVal args() As String)
        Timer.Interval = 2000
        Timer.Enabled = True
    End Sub
    Protected Overrides Sub OnStop()
        Timer.Dispose()
    End Sub
    Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
        'do something...but it never fires...
    End Sub
End Class
 
Hello Aia.

I switched to Threading.Timer since Forms.Timer nor Timers.Timer worked for me...but that one wasn't working either. I'm using VS2008 with 3.5...based on your suggestion I tried Timers.Timer again...and I have to say that I'm stupid!

The first thing I tried was to show a MsgBox at each tick...but that raised an error in the EventLog about that a service could not use a GUI if it's not in the UserInteractive-Mode (which is a pretty important task of that service). The second is that I hunted down the Tick-Event using instead a entry to the log...and look there, it fires one time only. And this was the point where it finally sunk in, I'm deactivating the Timer for the time of the Tick-Event for preventing a new tick while the other is still going on. This technique is working within a Form, but not within a service (for whatever reason).

Thanks for your help. :)

Bob*it's.finally.ticking*by
 
I am glad that I was able to help you, but I am curious. Why does your service contain an interactive section? Normally a second application would be created to interact with the service and would contain the interactive portions.
 
Sorry, that description was a little bit blurry.
It's not _that_ kind of interactive. It's a report tool which takes over a text-file with format markers, formats it and prints it or shows a preview. This preview window is what I meant with the GUI. I already thought about returning that document for preview to the calling application, but since it's a class based on PrintDocument I would need that class also in the calling application, making it ridiculous to pass it first to a service and then back (especially 'cause the document is created in the OnPrint-Method).

All I wanna do is hand the service the handle to a file, or the content itself and forget about it, knowing the service is doing what is necessary (printing, showing a preview, saving etc. etc.).

Bobby
 
I do not see why you need to have it as a system service unless it was being accessed as a service on a network and wanted to make sure it was running while the computer was running.
 
Ok. Its good that you managed to work out your problem. But the "preview" part you mentioned seems like an odd choice to include into the service. But as long as it does what you want it to.
 
Back
Top