T
TwistedPair
All,
Been trying to get a little program to work, and I think I'm really close,
but I'm afraid I need to be pointed in the right direction. The code below
will watch a directory for files created. If a file is created, it will
copy it off to another directory. This even works if a bunch of files are
created all at once, but after a few copies, the program dies for some
reason. I suspect it's an IO issue, but I wanted to check with the experts
here. Any ideas?
Imports System.ServiceProcess
Imports System.Threading
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Dim DestDirectory As String
Dim WatchDirectory As String
Dim SourceFilePath As String
Dim SourceFileName As String
Dim MyQueue As New Queue
Dim MyRetValue As New Queue
Private CopyF As New Thread(New System.Threading.ThreadStart(AddressOf
CopyFile))
Private Watchservice As New Thread(New
System.Threading.ThreadStart(AddressOf StartMonitor))
#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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Start the thread.
Watchservice.Start()
CopyF.Start()
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
Watchservice.Abort()
CopyF.Abort()
End Sub
Private Sub StartMonitor()
WatchDirectory =
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\CopyMon",
"Source", Nothing)
DestDirectory =
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\CopyMon",
"Destination", Nothing)
Dim watcher As New System.IO.FileSystemWatcher(WatchDirectory)
'Dim result
'result = Nothing
AddHandler watcher.Created, AddressOf logchange
'Dim SourceFileNameObject As IO.WaitForChangedResult
'Dim SourceFileName As String
'SourceFileName = SourceFileNameObject.Name
'MsgBox(SourceFileName)
watcher.InternalBufferSize.Equals(4000960)
watcher.EnableRaisingEvents = True
'result =
watcher.WaitForChanged(System.IO.WatcherChangeTypes.Created)
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Created Then
SourceFilePath = e.FullPath
SourceFileName = e.Name
'MsgBox(SourceFilePath)
'MsgBox(DestDirectory & "\" & SourceFileName)
'Add files to queue to be picked off in a new sub below
MyQueue.Enqueue(SourceFilePath)
'MyRetValue = queue.Synchronized(MyQueue)
'MsgBox(MyQueue.Count)
End If
End Sub
Private Sub CopyFile()
Do
If MyQueue.Count > 0 Then
'MsgBox("Starting to copy files hopefully")
'get files from the queue, and copy them to destination path
Dim DestPath As String
Dim DQedSrceFilePath As String
DQedSrceFilePath = MyQueue.Dequeue()
DestPath = DestDirectory & "\" & SourceFileName
My.Computer.FileSystem.CopyFile(DQedSrceFilePath, DestPath)
End If
Loop
End Sub
End Class
Been trying to get a little program to work, and I think I'm really close,
but I'm afraid I need to be pointed in the right direction. The code below
will watch a directory for files created. If a file is created, it will
copy it off to another directory. This even works if a bunch of files are
created all at once, but after a few copies, the program dies for some
reason. I suspect it's an IO issue, but I wanted to check with the experts
here. Any ideas?
Imports System.ServiceProcess
Imports System.Threading
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Dim DestDirectory As String
Dim WatchDirectory As String
Dim SourceFilePath As String
Dim SourceFileName As String
Dim MyQueue As New Queue
Dim MyRetValue As New Queue
Private CopyF As New Thread(New System.Threading.ThreadStart(AddressOf
CopyFile))
Private Watchservice As New Thread(New
System.Threading.ThreadStart(AddressOf StartMonitor))
#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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Start the thread.
Watchservice.Start()
CopyF.Start()
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
Watchservice.Abort()
CopyF.Abort()
End Sub
Private Sub StartMonitor()
WatchDirectory =
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\CopyMon",
"Source", Nothing)
DestDirectory =
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\CopyMon",
"Destination", Nothing)
Dim watcher As New System.IO.FileSystemWatcher(WatchDirectory)
'Dim result
'result = Nothing
AddHandler watcher.Created, AddressOf logchange
'Dim SourceFileNameObject As IO.WaitForChangedResult
'Dim SourceFileName As String
'SourceFileName = SourceFileNameObject.Name
'MsgBox(SourceFileName)
watcher.InternalBufferSize.Equals(4000960)
watcher.EnableRaisingEvents = True
'result =
watcher.WaitForChanged(System.IO.WatcherChangeTypes.Created)
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Created Then
SourceFilePath = e.FullPath
SourceFileName = e.Name
'MsgBox(SourceFilePath)
'MsgBox(DestDirectory & "\" & SourceFileName)
'Add files to queue to be picked off in a new sub below
MyQueue.Enqueue(SourceFilePath)
'MyRetValue = queue.Synchronized(MyQueue)
'MsgBox(MyQueue.Count)
End If
End Sub
Private Sub CopyFile()
Do
If MyQueue.Count > 0 Then
'MsgBox("Starting to copy files hopefully")
'get files from the queue, and copy them to destination path
Dim DestPath As String
Dim DQedSrceFilePath As String
DQedSrceFilePath = MyQueue.Dequeue()
DestPath = DestDirectory & "\" & SourceFileName
My.Computer.FileSystem.CopyFile(DQedSrceFilePath, DestPath)
End If
Loop
End Sub
End Class