FileSystemWatcher instances pointing to single method

I'm creating an application with SystemFileWatcher objects to monitor multiple folders that are created on a SQL Server folder.

the files are created by a trigger on the respective server.

the application has the following example entries on the app.config file.

<?xml version="1.0" encoding="utf-8" ?>

my current source is a follows:

Imports System.Xml
Imports System.Text
Imports System.IO
Imports System.Configuration
Imports System.Collections.Specialized
Imports System.Collections.ObjectModel
Imports System.Collections
Imports System.Reflection
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Management.Smo

Public Class Form1
Dim m_Proj As CRAXDRT.Application ' Create a new COM instance of the Crystal Reports
Dim WithEvents m_Report As CRAXDRT.Report ' The dynamically generated report

Dim textbox As TextBox
Dim lbl As Label
Dim grpBox As GroupBox
Dim chkBox As CheckBox
Dim fsw As FileSystemWatcher
Dim afsw As New ArrayList()

ReadOnly Property ServerName(ByVal strText As String) As String
Return strText.Substring(strText.IndexOf("=") + 1, strText.IndexOf(";Initial Catalog") - strText.IndexOf("=") - 1)
End Get
End Property

ReadOnly Property DBName(ByVal strText As String) As String
Return strText.Substring(strText.IndexOf("=", strText.IndexOf(";")) + 1, strText.IndexOf(";uid") - 1 - strText.IndexOf("=", strText.IndexOf(";") + 1))
End Get
End Property

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
For i As Integer = afsw.Count - 1 To 0 Step -1

For i As Integer = Controls.Count - 1 To 0 Step -1
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i As Integer = 0 To ConfigurationManager.AppSettings.Count - 1

grpBox = New GroupBox
grpBox.Location = New System.Drawing.Point(IIf(i Mod 2, 400, 50), IIf(i Mod 2, ((i - 1) * 40) + 8, (i * 40) + 8))
grpBox.Size = New System.Drawing.Size(283, 70)
grpBox.Name = "txt" + ConfigurationManager.AppSettings.GetKey(i)
grpBox.Text = ConfigurationManager.AppSettings.GetKey(i).ToUpper
grpBox.BackColor = Color.Wheat

chkBox = New System.Windows.Forms.CheckBox
chkBox.Size = New System.Drawing.Size(100, 20)
chkBox.Checked = True
chkBox.Text = "Running"
chkBox.Location = New System.Drawing.Point(20, 15)
chkBox.Name = "chk" + ConfigurationManager.AppSettings.GetKey(i) & "_" & i.ToString
AddHandler chkBox.CheckStateChanged, AddressOf chkChangeProcessing

fsw = New FileSystemWatcher("\stg" & ServerName(ConfigurationManager.AppSettings(i)) & "c$sqlwatch" & DBName(ConfigurationManager.AppSettings(i)), "*.txt")
fsw.EnableRaisingEvents = True
fsw.IncludeSubdirectories = False
fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite Or NotifyFilters.CreationTime
AddHandler fsw.Created, AddressOf fswCreateImage
ProcessExistingFiles(fsw, fsw.Path)
End Sub

Public Sub chkChangeProcessing(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim chkBox As CheckBox
Dim i As Integer
Dim fsw As FileSystemWatcher
chkBox = CType(sender, CheckBox)

chkBox.Text = (IIf(chkBox.CheckState, "Running", "Stopped"))
i = CInt(chkBox.Name.Substring(chkBox.Name.IndexOf("_") + 1, chkBox.Name.Length - chkBox.Name.IndexOf("_") - 1))

fsw = CType(afsw(i), FileSystemWatcher)
Select Case chkBox.Text
Case "Running"
fsw.EnableRaisingEvents = True
ProcessExistingFiles(fsw, fsw.Path)
Case "Stopped"
fsw.EnableRaisingEvents = False
End Select
Catch ex As Exception
End Try
End Sub

' Process existing files.
Private Sub ProcessExistingFiles(ByVal fsw As FileSystemWatcher, ByVal path As String)
Dim files As String() = Directory.GetFiles(path, "*.txt")
For Each s As String In files
fswCreateImage(fsw, New System.IO.FileSystemEventArgs(WatcherChangeTypes.All, "", s))
End Sub

Public Sub fswCreateImage(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs)
Dim fsw As FileSystemWatcher
fsw = CType(sender, FileSystemWatcher)

End Sub
End Class

i am not sure if the method "fswCreateImage" can handle the multiple calls from the instances of FileSystemWatcher given the Created events that can occur from the files created by the trigger.

any suggestions??
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!