2

I have:

using System.IO;
using System.Windows.Forms;

namespace myNamespace1
{
    public partial class Form1 : Form
    {
        FileSystemWatcher watcher = new FileSystemWatcher();

        public Form1()
        {
            InitializeComponent();

            watcher.Path = @"c:\users\Me\desktop\z";
            watcher.Created += new FileSystemEventHandler(watcher_Created);
            watcher.EnableRaisingEvents = true;
        }

        void watcher_Created(object sender, FileSystemEventArgs e)
        {
            Text = e.Name + " " + e.ChangeType.ToString();
        }

    }
}

When I add a folder or file to the folder (-z) – the program closes. Why?

I'm running in Debug mode. And I don't get any exception from VS.

EDIT:

The answer:

jon-skeet's answer

+(in a comment)

In Visual Studio, can you go to the Debug Menu -> Exceptions. On this dialog, make sure that next to Common Language Runtime Exceptions, make sure both 'Thrown' and 'User Unhandled' are ticked, click OK and try debugging again. – dash

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291

5 Answers5

2

Without knowing the details of what kind of program you created, I'm guessing you have an unhandled exception occurring somewhere in your code that is causing your application to close.

Update

After looking at your edit, it looks like Jon is correct (as usual). Your application is trying to update the Text property on the wrong thread. Your handler should really be:

Action a = () => e.Name + " " + e.ChangeType.ToString();
Invoke(a);
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • This is the whole program (I'm experimenting with `FileSystemWatcher`). And I'm running in Debug mode. And I don't get any exception from VS. – ispiro Dec 08 '11 at 16:11
  • 1
    @ispiro - This can't be the whole program (since I'm still not able to tell if it's Winforms, Console, etc.) and I have no idea what the property Text belongs to. – Justin Niessner Dec 08 '11 at 16:12
  • It clearly *isn't* your whole program - it doesn't declare a class, for example. If you could show a short but *complete* program demonstrating the problem, it would be easier for us to reproduce. – Jon Skeet Dec 08 '11 at 16:14
  • 1
    In Visual Studio, can you go to the Debug Menu -> Exceptions. On this dialog, make sure that next to Common Language Runtime Exceptions, make sure both 'Thrown' and 'User Unhandled' are ticked, click OK and try debugging again. – dash Dec 08 '11 at 16:24
2

Assuming Text is trying to change a UI property, you're changing the UI from the wrong thread. FileSystemWatcher raises events on thread-pool threads, but you're only meant to access the UI from the UI thread. That's probably throwing an exception in the thread-pool thread, which is bringing down the process.

Try this instead:

void watcher_Created(object sender, FileSystemEventArgs e)
{
    Action action = () => Text = e.Name + " " + e.ChangeType;
    // Or Dispatcher.Invoke - it depends on your application type
    Invoke(action);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

In addition to Justin's answer, you can only specifiy a directory for the Path property.

To monitor changes on a specific file, use the Filter property.

Also try adding some exception handling or stepping through the code in debug mode to see what is happening. Pay close attention to how you are setting the label, although I'd expect to see a cross thread exception.

Are you seeing any exceptions at all?

dash
  • 89,546
  • 4
  • 51
  • 71
  • Cool. If you see strange behaviour in your application and you suspect it's an exception, always check how you have the debugger setup to catch exceptions. You can easily miss things this way. However, you might not want to have all exceptions being set all the time, as it can make debugging *very* tedious. – dash Dec 08 '11 at 16:34
1

What's Text, and what's done with it when the event fires?

Bear in mind the FileSystemWatcher creates its own thread from the threadpool to handle events. Also, there is a good deal of unmanaged code under the hood of this class, which can result in unhandled exceptions being silently swallowed if you have "Just My Code" turned on in the debugger options.

You may want to create a producer/consumer model to handle this -- see After FileSystemWatcher fires - Thread Pool or Dedicated thread? for more details.

Community
  • 1
  • 1
Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
-1

Is it a windows service? Generally, if the file watcher couldnt find the folder or has no access to a folder, it dies. Did u check ur EventLog?

CodingSlayer
  • 883
  • 1
  • 7
  • 14