473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 "StartMonitor" 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.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

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(ServicesToRu n)

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)

Do

Dim watcher As New System.IO.FileSystemWatcher(WatchDirectory)

Dim result

AddHandler watcher.Created, AddressOf logchange

'Dim SourceFileNameObject As IO.WaitForChangedResult

'Dim SourceFileName As String

'SourceFileName = SourceFileNameObject.Name

'MsgBox(SourceFileName)

result = watcher.WaitForChanged(System.IO.WatcherChangeType s.Created)

Loop

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)

'MsgBox(MyQueue.Count)

End If

End Sub

Private Sub CopyFile()

Do

System.Threading.Thread.Sleep(60000)

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
Mar 26 '07 #1
0 1181

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: matthurne | last post by:
I am working through exercise 8-2 in Accelerated C++...I am implementing the function equal(b, e, b2) where b is an iterator for the first element in a container, e is an iterator pointing to one...
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
2
by: Stan | last post by:
I wasn't sure what would be the best ng to post it. We have ASP.NET app that uses serviced component to perform all database writes and updates (there are separate components sitting on the web...
5
by: Matthew Speed | last post by:
(About me: I know very little about writing server applications. I have done plenty of VB6 desktop app work but this is my first server program. I got it to work by modifying examples. I...
7
by: Mythran | last post by:
Been 11 days since I posted this and 0 replies (although, in OE, it looks like there was 1 but it's just another post with the same subject as before): Part #1: I have a Thread, MainThread, and...
4
by: John | last post by:
I'd like to write a programme that runs on a PC with two monitors. The application would be used in a shop, with one monitor for shop assistant and the other for the customer. The two must show...
2
by: bic1ster | last post by:
I go in through the Control Center Activity Control Center and eventually select view locks, I get a popup window "The Activity Monitor is unable to collect the required snapshot data because the...
23
by: sophia.agnes | last post by:
Dear all, I was going through a C book written by an indian author the following are the questions given in that book 1) consider the following statement: s1 : an operator may require an...
13
by: AliRezaGoogle | last post by:
Dear Members, I have a problem in the concepts of Monit.Enter and Monitor.Exit (Before everything I should say that I know how to solve this problem by using Monitor.Wait and I do not need a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.