Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSystemWatcher "created" event won't fire as a service - HE

Status
Not open for further replies.

modfather

MIS
Feb 15, 2000
75
US
I'm in *desperate* need for help. I'm a bit of a newbie, so please be gentle. :)

I'm trying write a vb.net program as a service. Essentially, all I want to do is have the FileSystemWatcher wait for files to be copied into a directory. As they are, any that have a .bmp, a .pcl, or a .inv extension should be moved to two different directories - one is on a different server. Right now, I'm copying, verifying, and deleting the original. But I have a few problems:

1. On startup of the service, I make a call to my method to copy the files. It works fine on startup. But as the service keeps running, if I copy files into the directory, the FileSystemWatcher doesn't seem to fire. Since I can't do something simple like show a messagebox, is there some way I can verify that my event is or isn't firing? Is there something I have to do different? When I created my FileSystemWatcher, I do put the name of the directory in the Path property. Is there something else I need to do? Aarrgh!

2. There is another software package that is trying to process and move these files. I want my program to pick these up and delete them before theirs. I can give a higher priority on the service, but is there a better method to move files? Since I'm copying to a different server, I can't move the files, right?

3. The way this is coded, if four files are copied into the directory, my event fires four times (regardless of how many files meet my criteria). Of course, as I said before, this is only working on startup of the service at this time. Because the event fires too many times, I try to copy the same files over and over again. I noticed that I can put a filter on the FileSystemWatcher I created, but can I list multiples? For instance "*.inv,*.bmp,*.pcl".

Here's my code:

...
Public Sub New()
MyBase.New()
moveFiles()
...

Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
moveFiles()
End Sub

Private Sub moveFiles()
Dim dt As Date = Date.Now
Dim myArray() As String
Dim i As Integer
Dim j As Integer
Dim myString As String
Dim destFile1 As String = "c:\program files\rxlaser\rxserver5 \bkandlog\"
Dim destFile2 As String = "\\rescamilla01\faxmove\"
Dim logFile As String = "c:\program files\rxlaser\rxserver5\bkandlog\faxmove" & dt.Day.ToString("00") & dt.Month.ToString("00") & dt.Year.ToString.Substring(2, 2) & ".txt"
Dim srcFilesLoc As String = "c:\program files\rxlaser\rxserver5\poll"
Dim fsSave As IO.FileStream
Dim srWriter As IO.StreamWriter
fsSave = New IO.FileStream(logFile, IO.FileMode.Append, IO.FileAccess.Write)
srWriter = New IO.StreamWriter(fsSave)
myArray = System.IO.Directory.GetFiles(srcFilesLoc)
For i = 0 To UBound(myArray)
If IO.Path.GetExtension(myArray(i)) = ".bmp" Or _
IO.Path.GetExtension(myArray(i)) = ".inv" Or _
IO.Path.GetExtension(myArray(i)) = ".pcl" Then
'MessageBox.Show(myArray(i))
Try
myString = dt.ToString & " File: " & myArray(i)
System.IO.File.Copy(myArray(i), destFile1 & System.IO.Path.GetFileName(myArray(i)))
System.IO.File.Copy(myArray(i), destFile2 & System.IO.Path.GetFileName(myArray(i)))
Catch ex As IO.IOException
srWriter.WriteLine(dt.ToString & " File: " & myArray(i) & " NOT copied - REASON: " & ex.Message)
Finally
myString = String.Concat(myString, " Successfully moved")
End Try
If (IO.File.Exists(destFile1 & IO.Path.GetFileName(myArray(i)))) Then And _
IO.File.Exists(destFile2 & IO.Path.GetFileName(myArray(i)))) Then 'delete original
srWriter.WriteLine(myString)
'IO.File.Delete(myArray(i))
End If
End If
Next

srWriter.Close()
fsSave.Close()
End Sub

If anyone can help me, I'd really appreciate it. It'd guarantee you a nice Christmas, Hannukah, Kwanzaa or non-holiday holiday card. :)

Thanks very much!
Steve

P.S. Any other general help or criticisms with my sphaghetti code would be warmly welcomed!
 
Steve,

I have been having the fun or getting windows services to run and do kinda what you want them to do. After much messing around i use a different method to do my windows services. I create a base class that has all of the onstart onstop,etc services commands then in he main service i overload those events in the class

Public Class PDFPrint
Inherits System.ServiceProcess.ServiceBase
Private WithEvents pdfprnt As PDFPrintBase

Protected Overrides Sub OnStart(ByVal args() As String)
pdfprnt = New PDFPrintBase()
pdfprnt.DebugMode = False
pdfprnt.OnStart(args)
MyBase.OnStart(args)
End Sub

Protected Overrides Sub OnStop()
If Not pdfprnt Is Nothing Then
pdfprnt.OnStop()
End If
pdfprnt.Dispose()
pdfprnt = Nothing
MyBase.OnStop()
End Sub

Then in the class i have timer that starts up when the onstart is called
Public Sub OnStart(ByRef args() As String)

myTimer.Start()

RaiseEvent EventLog("PDFPrint Service Started. Timer interval: " & myTimer.Interval.ToString, EventLogEntryType.Information, 0, 0, Nothing)
End Sub

then i have a handler on the timer which gets set when they call the new routine of the class

Public Sub New()
AddHandler myTimer.Elapsed, AddressOf OnTimedEvent
End Sub

then in the ontimedevent i do whatever work need to be done and have a boolean that i set so that i know that i am already doing the processing.

Public Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
If m_bIsProcessing Then
Exit Sub
End If
Try
m_bIsProcessing = True
Dim strConn As String
Dim myCon As SqlClient.SqlConnection = New SqlClient.SqlConnection(strConn)
myCon.Open()

myCon.Close()
myCon = Nothing
m_bIsProcessing = False
Catch ex As Exception
m_bIsProcessing = False
RaiseEvent EventLog(ex.Message, EventLogEntryType.Error, 112, 1, Nothing)
End Try

End Sub
That stops you from attempting to do the same thing twice. it also allows you to handle any issues with pausing and stopping of the service.
HTH,
Bueller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top