Alex Rivera | Logout

How do I get this event-based console app to not terminate immediately?

Asked 2011-12-30T04:10:49.427
18

Source

class Program
{
    static void Main(string[] args)
    {
        var fw = new FileSystemWatcher(@"M:\Videos\Unsorted");
        fw.Created+= fw_Created;
    }

    static void fw_Created(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("added file {0}", e.Name);
    }
}

Question

Should be pretty self explanatory. I'm trying to create a file watcher so I can sort my videos for me automatically...how do I get the program to not terminate, ever?

I want to keep it console-based for now so I can debug it, but eventually I want to remove the console and just have it run in the background (I guess as a service).

Edit
Report

1 Answer

2

Console apps are never a good way to test events due to this scenario. Whatever method you use, it has to halt the current thread, either sleep or lock in some while(true) loop, which pevents your events from firing or makes it almost impossible to hit breakpoints inside events. Use a windows app if you can.

answered 2011-12-30T04:24:02.400

Your Answer