Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Service

Status
Not open for further replies.

SQLScholar

Programmer
Aug 21, 2002
2,127
GB
Hey all,

I am making my first service, but am having a couple of issues understanding how it works.

I have

Code:
        Try
            Const iTIME_INTERVAL As Integer = 60000      ' 60 seconds.
            Dim oTimer As System.Threading.Timer

            System.IO.File.AppendAllText("C:\AuthorLog.txt", "AuthorLogService has been started at " & Now.ToString())


            Dim tDelegate As Threading.TimerCallback = AddressOf EventAction
            oTimer = New System.Threading.Timer(tDelegate, Me, 0, iTIME_INTERVAL)
        Catch
        End Try

If the otimer = new system.threading fails, it just stops the process. This is making it incredibly difficult to debug any issues. How can i handle this correctly so that it doesnt just die?

Many thanks

Dan

----------------------------------------
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Dr. Seuss

Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
 
I would tend to place a timer control on the class at a global scope.

Then set it's interval and enable it when the service starts.

Making it local tends to limit what you can expect.


You could even potentially add a second timer and have it watch the status of the first.

ALso adding it with global scope provides you the ability to define it with events and then add the exection code to the event block for the timer.

ROb
 
It is best not to use the timer control. You should delare the timer;
Code:
Public tmrRun As New System.Timers.Timer()

Protected Overrides Sub OnStart(ByVal args() As String)
        tmrRun.Interval = 60000
        tmrRun.Enabled = True
'add the code to handle the tick event
        AddHandler tmrRun.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Me.tmrRun_Elapsed)
'your startup code here if needed
End Sub

 Private Sub tmrRun_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)
'do what the service should do
End Sub
 
It is best not to use the timer control. You should delare the timer;

He's not using the timer control; he's using the threaded timer. But I agree, as I replied in one of his other posts, in this case, the server timer should be fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top