2 Filesystem Watchers or Get out of loop in 1 watcher

Mimoa

Member
Joined
Apr 29, 2020
Messages
24
Programming Experience
1-3
Hello.
I have a simple simulator - it starts a Filesystem watcher and watch one directory for new files. The file comes and when it has the right name, watcher runs function. This function is loop Do-While and it generates files for other program (the one that create the first file...). Now, everything is fine - file comes, watcher runs function and it generates file for each 5 seconds. OK.
Now I need either another watcher to watch over another file in the same directory or I need to start the function for file generation but outside of the watcher to make the watcher free to watch again the directory over.
How can it be done?
When I choose 2 watchers - they will collide, when the file is created in watched folder.


C#:
        public void FileWatcher()   // watches BaseDir = folder s programem *.exe
        {
            WatcherON = true;
            if (File.Exists("Status.JOB")) { File.Delete("Status.JOB"); }
            if (File.Exists("Cykly.JOB")) { File.Delete("Cykly.JOB"); }
            Watcher = new FileSystemWatcher();
            Watcher.Path = AppDomain.CurrentDomain.BaseDirectory;
            Watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastAccess | NotifyFilters.LastWrite;
            Watcher.Filter = "*.JOB";                                                           
            Watcher.Created += new FileSystemEventHandler(FChange);
            Watcher.EnableRaisingEvents = true;
        }
        public void FChange(object source, FileSystemEventArgs e)
        {
            Thread.Sleep(200);
            string strFile = e.Name;


            if (strFile == "Cykly.JOB" && LBINFO.Text == "AUTOMAT")
            {
                Thread.Sleep(200);
                if (File.Exists("Cykly.JOB")) { File.Delete("Cykly.JOB"); }
                if (File.Exists("Cykly.dat")) { File.Delete("Cykly.dat"); }
                do
                {
                    if (File.Exists("Cykly.dat")) { File.Delete("Cykly.dat"); }
                    using (StreamWriter PDVF = File.CreateText("Cykly.dat"))
                    {
                        Random Rand = new Random();
                        PDVF.WriteLine("NUR");
                        PDVF.WriteLine(Rand.Next(0, 100000));
                        PDVF.Close();
                        Thread.Sleep(5000);
                    }
                }
                while (generate);
                try
                {
                    using (FileStream PDVF = File.OpenRead("Cykly.dat"))
                    {
                        PDVF.Close();
                    }
                }
                catch { }
            }


            if (strFile == "Status.JOB")
            {
                using (StreamWriter PDVF = File.CreateText("Status.dat"))
                {
                  
                    PDVF.WriteLine("NUR");
                    if (LBINFO.Text =="AUTOMAT") PDVF.WriteLine("0A");
                    if (LBINFO.Text == "RUCNI") PDVF.WriteLine("0M");
                    if (LBINFO.Text == "SERIZOVANI") PDVF.WriteLine("0U");
                    PDVF.Close();
                }

            }
        }
 
Back
Top Bottom