0

I have a FileSystemWatchers monitoring 1 location in a Start/stop Button, but I can“t manage to stop it, the button works pretty much well as when I click on it, it reads the code when it is off (the text changes to "start Watching") but it does not manage to stop monitoring, also I have tried "watcher.EnableRaisingEvents = false;" but it does not work could you please help me??

using System
    using System.ComponentModel
    using System.Diagnostics
    using System.Drawing
    using System.IO
    using System.Windows.Forms
    
    namespace Watcher
    {
        public partial class Form1 : Form
        {
            private bool isWatching
            bool on = true
            bool togglelight = true
            Timer t = new Timer()
            public Form1()
            {
                InitializeComponent()
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                if (isWatching)
                {
                    stopWatching()
                }
                else
                {
                    startWatching()
                }
            }
    
            private void startWatching()
            {
                isWatching = true
                button1.Text = "Stop Watching"
                t.Start()
                on = false;
                var watcher = new FileSystemWatcher(@"C:\location1")
                
                watcher.NotifyFilter = NotifyFilters.CreationTime
                | NotifyFilters.CreationTime
                | NotifyFilters.FileName
    
                watcher.Changed += OnChanged
                
                watcher.Error += OnError
    
                watcher.Filter = "*.xlsx"
                watcher.IncludeSubdirectories = false
                watcher.EnableRaisingEvents = true
    
            private void stopWatching()
            {
                isWatching = false;
                button1.Text = "Start Watching"
                button1.BackColor = Color.Gray
                t.Enabled = false
                t.Stop()
                on = true
            }
    
            private static void OnChanged(object sender, FileSystemEventArgs e)
            {
                if (e.ChangeType != WatcherChangeTypes.Changed)
                {
                    return
                }
    
                Console.WriteLine(DateTime.Now + "   tipo de cambio: " + 
                e.ChangeType + ".    " + e.FullPath)
                kNime_Bat(@" C:\Users\BAT\Knime_eSTORE.bat")
                Console.WriteLine(DateTime.Now + " se proceso correctamente")
            }
    
            private static void OnError(object sender, ErrorEventArgs e) =>
                PrintException(e.GetException())
    
            private static void PrintException(Exception ex)
            {
                if (ex != null)
                {
                    Console.WriteLine($"Message: {ex.Message}")
                    Console.WriteLine("Stacktrace:")
                    Console.WriteLine(ex.StackTrace)
                    Console.WriteLine()
                    PrintException(ex.InnerException)
                }
            }
            private static void kNime_Bat(string ruta_del_archivoBat_knime)
            {
                try
                {
                    ProcessStartInfo psi = new ProcessStartInfo()
                    psi.UseShellExecute = false
                    psi.CreateNoWindow = true
                    psi.WindowStyle = ProcessWindowStyle.Hidden
                    psi.FileName = ruta_del_archivoBat_knime
                    Process.Start(psi)
                }
    
                catch (Win32Exception)
                {
    
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                button1.Text = "Start Watching"
                t.Interval = 1000
                t.Tick += new EventHandler(t_Tick)
            }
    
            private void t_Tick(object sender, EventArgs e)
            {   
                if (togglelight)
                {
                    button1.BackColor = Color.DarkBlue
                    togglelight = false
                }
                else
                {
                    button1.BackColor = Color.Gray
                    togglelight = true
                }
            }
        }
    }

I have tried watcher.EnableRaisingEvents = false in Stopwatching event but it seems like if it works in a new instance as it does not stop the watcher, how I know? because after I click on "stop" I make a new change in the watched folder and I get an answer in OnChanged event.

Fer
  • 13
  • 3

0 Answers0