Problem with FileSystemWatcher not firing the created event in a Windows Service

  • Thread starter Thread starter Chetan via DotNetMonster.com
  • Start date Start date
C

Chetan via DotNetMonster.com

Hi,
I have the following snippet of code from my windows service which has a
FileSystemWatcher component that monitors a particular folder.As soon as
new files are dropped into this folder the event handler for the created
event has code which inserts the name of the file into a database and sets
the column called "processed" to "NO".Then the last line of the created
event handler calls ProcessFile() which selects all those files from the
database table where "processed"="NO" and in turn calls another method to
process these files.This is done because I assumed that the
FileSystemWatcher will not be able to handle the dropping of multiple files
at the same time into the "watched" directory.

I attached the VS.NET Debugger to the service and then dropped a file into
the "watched" directory using drag and drop.But the created event was not
fired.Any help will surely be appreciated.

Here is the code snippet:
private void InitializeComponent()
{
//Other Components

((System.ComponentModel.ISupportInitialize).fileSystemWatcher1)).BeginInit()
;
//
// fileSystemWatcher1
//
this.fileSystemWatcher1.EnableRaisingEvents = true;
this.fileSystemWatcher1.Created+=new FileSystemEventHandler
(this.fileSystemWatcher1_Created);
//
// Scheduler
//
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "Scheduler";
((System.ComponentModel.ISupportInitialize)this.fileSystemWatcher1))
..EndInit();
}

protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("Scheduler Started");
//Begin monitoring
fileSystemWatcher1.EnableRaisingEvents=true;
//Set the filter type property
fileSystemWatcher1.Filter="*.*";
//Set the path of the directory to watch
fileSystemWatcher1.Path=System.Configuration.ConfigurationSettings.AppSettings
["FileDirectory"];
//Set the NotifyFilters property for raising events
fileSystemWatcher1.NotifyFilter=NotifyFilters.LastWrite|NotifyFilters.LastAccess|NotifyFilters.Size;
//Determine whether FileSystemWatcher object should watch subdirectories
fileSystemWatcher1.IncludeSubdirectories=false;
}
private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs
e)
{
eventLog1.WriteEntry("Scheduler:Started processing");
OleDbConnection conn = new OleDbConnection();
//set ConnectionString
string sSQL=//Select query
OleDbCommand oledbselectcmd=new OleDbCommand(sSQL,conn);
string iSQL=//Insert query
OleDbCommand oledbcmd=new OleDbCommand(iSQL,conn);
object queryResult;
try
{
conn.Open();
// Insert code to process data.
queryResult=oledbselectcmd.ExecuteScalar();
if(queryResult==null)
oledbcmd.ExecuteNonQuery();
}
catch (Exception)
{
}
finally
{
conn.Close();
}
ProcessFile();
}
 
Back
Top