473,772 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

4
1892
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 past the last element in that same container, and b2 is an iterator for the first element in the second container. Here's what I have: template <class T> bool equal(T begin, T end, T begin2) { while (begin != end) { if (*begin != *begin2) {
1
4242
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. From the log it looks like the problem happens when 2 threads insert 1 record each in the same table and then try to aquire a NS (Next Key Share) lock on the record inserterd by the other thread. Thanks Rohit
2
1338
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 server to do reads) Initially, serviced components and ASP.NET app were on two separated machines and components had server activation Since there is a substantial overhead in running serviced compoents as server (dllhost, interop, marchaling,...
5
1538
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 understand what it is doing but not much about what is involved in extending it. ) I am not necessarily looking for code examples here, just some pointers as to what direction I need to proceed to resolve these things. I have written a...
7
1201
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 a child thread that is started in the MainThread called ChildThread. I have a DataSet created in the MainThread in which needs to be passed to the ChildThread. The ChildThread needs to update the DataSet with new data.
4
2390
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 things independently, so the application can display certains things on one monitor (eg goods in the store) and certain other things on the other monitor (eg details of a certain product). Is this possible?
2
2442
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 monitor switch "LOCK" is not turned on at the database manager level." I had run db2 update monitor switches using BUFFERPOOL on LOCK on SORT on STATEMENT on TIMESTAMP on TABLE on UOW on Does DB2 need to be restarted? Does das need a restart?
23
2767
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 lvalue operand, yet yield an r value s2: an operator may accept an rvalue operand , yet gives an lvalue
13
2681
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 solution. But this question sticks to my head as a conceptual problem) Suppose there are two threads T1, T2 running concurrently:
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.