|
-
Mar 25th, 2004, 01:30 PM
#1
Thread Starter
Member
.Net Windows Service Woes
I have a service based on an example I found.
Has two problems
1. It pegs the CPU 100%. I tried a timer no luck. Maybe I am not using the timer right, I dont know.
2.It writes too many log entries. for example if a file is created it is
tio run the OnCreate sub. Instead it runs it plus the OnChange sub four times.
Any clues?
Imports System.ServiceProcess
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.IO
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Dim t1 As New Thread(AddressOf Listen)
Dim canStopListening As Boolean = False
Dim pause As Boolean = False
Dim log As New System.Diagnostics.EventLog
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
t1.Start()
Me.AutoLog = False
If Not EventLog.SourceExists("WindChill To JDE Service Log") Then
EventLog.CreateEventSource("Windchill To JDE Service Log", "WindchillToJDELog")
End If
log.Source = "WindChill To JDE Service Log"
log.WriteEntry("Service Started")
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
' Invoked when the service is stopped
canStopListening = True
log.WriteEntry("Service Stopped")
End Sub
Protected Overrides Sub OnPause()
' Invoked when the service is paused
pause = True
log.WriteEntry("Service Paused")
End Sub
Protected Overrides Sub OnContinue()
' Invoked when the service is continued
pause = False
log.WriteEntry("Service Continued")
End Sub
Private Sub listen()
Dim watcher As New FileSystemWatcher
watcher.Path = "C:\"
watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
' Only watch xml files.
watcher.Filter = "*.xml"
' Add event handlers.
AddHandler watcher.Created, AddressOf OnCreated
AddHandler watcher.Changed, AddressOf OnChanged
AddHandler watcher.Deleted, AddressOf OnDeleted
AddHandler watcher.Renamed, AddressOf OnRenamed
Do
If Not pause Then
watcher.EnableRaisingEvents = True
End If
Loop Until canStopListening
End Sub
Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
log.WriteEntry("File: " & e.FullPath & " " & e.ChangeType & "Changed")
End Sub
Private Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
' Specify what is done when a file is changed, created, or deleted.
log.WriteEntry("File: " & e.FullPath & " " & e.ChangeType & "Created")
End Sub
Private Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
log.WriteEntry("File: {0} renamed to {1}" & e.OldFullPath, e.FullPath & "Renamed")
End Sub
Private Sub OnDeleted(ByVal source As Object, ByVal e As FileSystemEventArgs)
log.WriteEntry("File: " & e.FullPath & " " & e.ChangeType & "Deleted")
End Sub
End Class
-
Mar 25th, 2004, 01:41 PM
#2
Thread Starter
Member
Ok found out one thing
I found out that the multiple log writes is normal. I also
replaced:
Do
If Not pause Then
watcher.EnableRaisingEvents = True
End If
Loop Until canStopListening
With:
watcher.EnableRaisingEvents = True
Do
Loop Until canStopListening
Still pegging at 100% CPU
-
Mar 25th, 2004, 02:18 PM
#3
Thread Starter
Member
Ok got it
Put this in:
Do
watcher.EnableRaisingEvents = True 'this is to slow it down so we dont peg the CPU
Thread.CurrentThread.Sleep(10000)
Loop Until canStopListening
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|