More questions on monitor/copy program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TwistedPair

    More questions on monitor/copy program

    All,
    I had some great advice about this a bit ago, but I'm just not good enough
    with this code to put together all the pieces. The way the code below works
    is as a service. When it is started, it watches a folder that is set in a
    registry key, and if there is a file created in that folder, it copies it to
    a destination folder, also defined in a registry key. This works fine, for
    individual files, and small files, but as mentioned before, if I were to
    create a bunch of multiple files almost simulteneously, it only copies the
    first one.

    I was given some great advice that I should separate the program into
    threads, one that listens to the source-dir, and any file that gets created
    gets added to a queue. The second thread should basically dequeue anything
    in the queue. Well I THINK I was able to separate those two procedures into
    their own threads, but I must have done something incorrectly, because it
    still can't cope with it when I drop several files in the directory.

    Can someone help me get over this one? I was thinking that if I could
    somehow put the stuff in the "logchange" sub into the "StartMonit or" sub,
    that I might be able to gain something, but at this point I don't see how
    that would make any difference.


    Imports System.ServiceP rocess

    Imports System.Threadin g

    Public Class Service1

    Inherits System.ServiceP rocess.ServiceB ase

    Dim DestDirectory As String

    Dim WatchDirectory As String

    Dim SourceFilePath As String

    Dim SourceFileName As String

    Dim MyQueue As New Queue

    Private CopyF As New Thread(New System.Threadin g.ThreadStart(A ddressOf
    CopyFile))

    Private Watchservice As New Thread(New
    System.Threadin g.ThreadStart(A ddressOf StartMonitor))

    #Region " Component Designer generated code "

    Public Sub New()

    MyBase.New()

    ' This call is required by the Component Designer.

    InitializeCompo nent()

    ' Add any initialization after the InitializeCompo nent() 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.Disp ose()

    End If

    End If

    MyBase.Dispose( disposing)

    End Sub

    ' The main entry point for the process

    <MTAThread()_

    Shared Sub Main()

    Dim ServicesToRun() As System.ServiceP rocess.ServiceB ase

    ' 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.ServiceP rocess.ServiceB ase () {New Service1,
    New MySecondUserSer vice}

    '

    ServicesToRun = New System.ServiceP rocess.ServiceB ase() {New Service1}

    System.ServiceP rocess.ServiceB ase.Run(Service sToRun)

    End Sub

    'Required by the Component Designer

    Private components As System.Componen tModel.IContain er

    ' 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.Diagnos tics.DebuggerSt epThrough()Priv ate Sub InitializeCompo nent()

    components = New System.Componen tModel.Containe r()

    Me.ServiceName = "Service1"

    End Sub

    #End Region

    Protected Overrides Sub OnStart(ByVal args() As String)

    ' Start the thread.

    Watchservice.St art()

    CopyF.Start()

    End Sub

    Protected Overrides Sub OnStop()

    ' Add code here to perform any tear-down necessary to stop your service.

    Watchservice.Ab ort()

    CopyF.Abort()

    End Sub

    Private Sub StartMonitor()

    WatchDirectory =
    My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\C opyMon",
    "Source", Nothing)

    DestDirectory =
    My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\C opyMon",
    "Destinatio n", Nothing)

    Do

    Dim watcher As New System.IO.FileS ystemWatcher(Wa tchDirectory)

    Dim result

    AddHandler watcher.Created , AddressOf logchange

    'Dim SourceFileNameO bject As IO.WaitForChang edResult

    'Dim SourceFileName As String

    'SourceFileName = SourceFileNameO bject.Name

    'MsgBox(SourceF ileName)



    result = watcher.WaitFor Changed(System. IO.WatcherChang eTypes.Created)

    Loop

    End Sub

    Private Sub logchange(ByVal source As Object, ByVal e As
    System.IO.FileS ystemEventArgs)



    If e.ChangeType = IO.WatcherChang eTypes.Created Then

    SourceFilePath = e.FullPath

    SourceFileName = e.Name

    'MsgBox(SourceF ilePath)

    'MsgBox(DestDir ectory & "\" & SourceFileName)

    'Add files to queue to be picked off in a new sub below



    MyQueue.Enqueue (SourceFilePath )

    'MsgBox(MyQueue .Count)

    End If

    End Sub

    Private Sub CopyFile()

    Do

    System.Threadin g.Thread.Sleep( 60000)

    If MyQueue.Count 0 Then

    'MsgBox("Starti ng to copy files hopefully")

    'get files from the queue, and copy them to destination path

    Dim DestPath As String

    Dim DQedSrceFilePat h As String

    DQedSrceFilePat h = MyQueue.Dequeue ()

    DestPath = DestDirectory & "\" & SourceFileName

    My.Computer.Fil eSystem.CopyFil e(DQedSrceFileP ath, DestPath)

    End If

    Loop

    End Sub

    End Class


Working...