0

i filewatching a dir for deletion, addition and change. I started with addition. The processing time forces me to have a queue management otherwise i'll skip a lot of files added in the dir. Here is the code : Declaration :

static ConcurrentQueue<string> q = new ConcurrentQueue<string>();

Then in the Form_load :

Task.Factory.StartNew(filesAdded);

the Filewatcher function :

private void fileSystemWatcher1_Created_1(object sender, FileSystemEventArgs e)
    {
        q.Enqueue(e.FullPath);
    }

And then the filesAdded function :

        void filesAdded ()
    {
        bool run = true;
        AppDomain.CurrentDomain.DomainUnload += (s, e) =>
        {
            run = false;
            q.Enqueue("stop");
        };
        while (run)
        {
            string filename;
            if (q.TryDequeue(out filename) && run)
            {
                Globals.getTags(filename);
                Globals.ecritDs(filename, true);
            }
        }
    }

credit goes to Rene here

it's working fine but i'm struggling to find a way to use the same mechanism for modified & deleted files.

 private void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e)
    {}
 private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
    {}

I would like to get the trigger info in the filesAdded function (that obviously needs to be renamed) but i have no idea how to do it since the Enqueue method takes only one parameter. Any help is appreciated !

  • 1
    Instead of a ConcurrentQueue use a ConcurrentQueue and then define a class that holds the information you require for processing. Btw, FileSystemWatcher has a buffer that by default is relatively small. You may just be able to increase the size and not drop the events – pinkfloydx33 May 22 '20 at 21:55
  • ahhh didnt think about it ! thanks :) – Sebastien Chemouny May 22 '20 at 22:26

1 Answers1

0

Thanks to pinkfloydx33, i'm using a class :

 private class FileWatched
    {
        public string fp { get; set; }
        public string trigger { get; set; }
    }

and then in the filesAdded loop :

 while (run)
        {
            if (q.TryDequeue(out fw) && run)
            {
                switch (fw.trigger)
                    {
                    case "created":
                        Globals.getTags(fw.fp);
                        Globals.ecritDs(fw.fp, true);
                        break;
                    case "modified":
                        break;
                    case "deleted":
                        break;
                    }
            }
        }