Migrating Console App To Windows Service

J

Jeremy S.

I have created a small console app that creates a few custom objects - each
of which makes use of the System.IO.FileSystemWatcher class. Each object is
responsible for monitoring a different folder on the local machine.

Here is the P-Code for the current console app version
static void Main() {
// 1 - Read the app's config file to determine which folders to watch

// 2 - Instantiate instances of my custom watcher class - one instance per
folder watched (and set various properties of each object) - then add each
instance to a HybridDictionary.

// 3 - KEEP THIS APP RUNNING until I explicitly end it
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');

} // END OF P-CODE

What do I do about step 3 when converting this console app to a Windows
Service? How do I ensure that the program will run until I explicitly shut
it down (i.e., I don't want it to execute from top to bottom and then exit
immediately - as the console app would do without the two lines listed above
in step 3).

Thanks!
 
P

Patrick Steele [MVP]

I have created a small console app that creates a few custom objects - each
of which makes use of the System.IO.FileSystemWatcher class. Each object is
responsible for monitoring a different folder on the local machine.

Here is the P-Code for the current console app version
static void Main() {
// 1 - Read the app's config file to determine which folders to watch

// 2 - Instantiate instances of my custom watcher class - one instance per
folder watched (and set various properties of each object) - then add each
instance to a HybridDictionary.

// 3 - KEEP THIS APP RUNNING until I explicitly end it
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');

} // END OF P-CODE

What do I do about step 3 when converting this console app to a Windows
Service? How do I ensure that the program will run until I explicitly shut
it down (i.e., I don't want it to execute from top to bottom and then exit
immediately - as the console app would do without the two lines listed above
in step 3).

You could put each FileSystemWatcher in a separate thread and use the
ManualResetEvent along with the WaitAll method to pause the main thread
until the other threads have completed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top