0

I have build a Windows service which keeps listing the events raised by files on different folder location with a FileSystemWatcher. I am getting the list of folder location from SQL database in OnStart method as code of OnStart is shown below.

public void OnStart()
{               
    try
    {
        // get the folder location list from Database
        var getListOfFOlderLocation = DbHelper.GetFolderList();

        // Get the maximum number of concurrent processes that can be active (if specified)
        if (ConfigurationManager.AppSettings["maxConcurrentProcesses"] != null)
            maxConcurrentProcesses = Convert.ToUInt32(ConfigurationManager.AppSettings["maxConcurrentProcesses"]);
                
        // Instantiate the regulation semaphore to enforce the maximum process count
        executionRegulator = new SemaphoreCounter(maxConcurrentProcesses);

        // System.Diagnostics.Debugger.Launch();
        // loop though in multiple directories to monitor files 
        foreach (string sMonitorFolder in DirectoryToWatch())
        {
            // Create and instantiate an individual FileSystemWatcher for each file set
            FileSystemWatcher fileSysetmWatcher = new System.IO.FileSystemWatcher(sMonitorFolder);

            if (Directory.Exists(sMonitorFolder))//make sure that mentioned directories exists
            { 
                fileSysetmWatcher.Path = sMonitorFolder;
                fileSysetmWatcher.Filter = "*.*";
                fileSysetmWatcher.IncludeSubdirectories = true;
                fileSysetmWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.DirectoryName;
                fileSysetmWatcher.Created += new FileSystemEventHandler(fileSysetmWatcher_created);
                fileSysetmWatcher.EnableRaisingEvents = true;
            }
        }

        new System.Threading.AutoResetEvent(false).WaitOne();
    }
    catch (Exception ex)
    {
        Global.WriteLog("Error: " + ex.Message);
    }
}
     

Everything is working fine and I am getting updates if files arrive at particular location.

Problem/Requirements: suppose service is running on a server and now I wanted to add more folders to watch than how do I let know service that more folder list has been updated in this table.

Please let me know if anyone did this before.

As of now after updating the table I am doing start/stop service in order to get updated folder watch list, which is obviously not good option.

I am looking for best way to do this.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Virender Thakur
  • 421
  • 7
  • 23
  • The mechanism to inform the service is up to you, how would you intercomunicate two processes? There are tons of ways to do it: pipes, TCP, UDP, message queues, etc. Even in your case you can have a special file in the folder you're supervising that contains a list of additional folders and when it's updated you read it and add the new fodlers listed in the file... – Gusman Sep 25 '20 at 12:46
  • `new System.Threading.AutoResetEvent(false).WaitOne();`??? What's _that_ supposed to do? – Fildor Sep 25 '20 at 12:48
  • 1
    @Fildor Avoids the "OnStart" function to end... as he hasn't stored a reference to the watchers when the "OnStart" function ends the garbage collector is disposing all those objects... Also, that will prevent the service to be started properly... That code is asking for a total rewrite.. – Gusman Sep 25 '20 at 12:54
  • 1
    @Gusman Exactly. So: _"Everything is working fine and I am getting updates if files arrive at particular location."_ - Cannot really believe that ... Not if this runs any longer than some minutes. – Fildor Sep 25 '20 at 13:09
  • @Gusman yes that is not the actual code as I dnt have access of code right now, but I have created a demo console application to demonstrate my situation and what I want to achieve. – Virender Thakur Sep 25 '20 at 13:56
  • After some research, I decided to use using SqlTableDependency to notify my service. I have used the refernce from here https://stackoverflow.com/questions/5288434/how-to-monitor-sql-server-table-changes-by-using-c/45690344. – Virender Thakur Sep 28 '20 at 04:51

0 Answers0