Afternoon (UTC +0) all

I am having a slight problem with the FileSystemWatcher.
The File watcher does exactly what i want it to do (as in throwing an event when needed).
However, when this event is thrown the debugger stops with no exceptions.

This has never happened to me before so i wondered if anyone could help me.

This is the function for creating the watcher:

private void CreateWatcher()
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = @"M:\CALL CENTRE\";
            watcher.Filter = "*.accdb";
            watcher.NotifyFilter = NotifyFilters.LastAccess;
            watcher.Changed += new FileSystemEventHandler(watcher_Changed);

            watcher.EnableRaisingEvents = true;
        }

And this is the "watcher_Changed" event:

        private void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            try
            {
                sup.ForeColor = Color.SteelBlue;
                sup.Text = "Updating...";

                sadset.Clear();
                admin.Fill(sadset.Tables["Solicminor"]);
                adlqu.Fill(sadset.Tables["Soliclqu"]);
                adsol.Fill(sadset.Tables["Solic"]);

                sup.ForeColor = Color.SeaGreen;
                sup.Text = "Updated";
            }
            catch
            {
                sup.ForeColor = Color.MistyRose;
                sup.Text = "Unable to update";
            }
        }

Thank you in advance

There is an exception being thrown, you can see it by changing your event handler:

private void watcher_Changed(object sender, FileSystemEventArgs e)
{
    try
    {
        ...
    }
    catch(Exception exc)
    {
        MessageBox.Show(exc.ToString());
        //Or Debug.WriteLine(exc.ToString());
        ...
    }
}

The issue is that the FileSystemWatcher fires the event from a different thread then the UI thread. WinForms Control's properties can only be accessed directly from a single thread, which helps prevent concurrency issues. You can use delegates to execute methods on the UI thread, for instance you could do something like this (assuming these methods are in your Form class):

private delegate void watcher_ChangedHandler(object sender, FileSystemEventArgs e);
private void watcher_Changed(object sender, FileSystemEventArgs e)
{
    if (InvokeRequired)
        Invoke(new watcher_ChangedHandler(watcher_Changed), new object[] { sender, e });
    else
        try
        {
            ...
        }
        catch
        {
            ...
        }
}

This simply uses a delegate to call itself. InvokeRequired will return false when the delegate is invoked on the UI thread, so it can continue executing in the else case. It is recommended to execute anything that is going to take time on another thread however. So you could either break this up a bit (I wouldn't recommend it though) or use a BackgroundWorker. The benefit of using the BackgroundWorker is that the DoWork event is called on a separate thread and the ProgressChanged is called on the UI thread, which allows you to update the UI.

awesome! thank you for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.