Consider the following code in a class called Worker
FileSystemWatcher watcher = new FileSystemWatcher();
public void Run()
{
watcher.Path = @"c:\queue";
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.EnableRaisingEvents = true;
}
Now here's what I would like to do wih the Worker, but obviouly this doesn't fly.
Worker worker = new Worker();
Thread thread = new Thread(worker.Run);
thread.Start();
Console.ReadLine(); // something else more interesting would go here.
The reason being that the Run method ends rather than going into some sort of event loop. How do I get an event loop going. (I think I"m looking for something like Application.Run)
Best Solution
Not tested, but your "event" loop, if you're wanting it to run on the thread, would look something like this:
You would place this code in a class and run it on a separate thread (if you wish). If you want to have your main thread wait on the thread you create finishing (although why bother creating a thread in that case?) then you can use the Thread.Join method.