I did this for an application that was supposed to process files as they were deleted in the folder. It is best to use a timer (as suggested) with Console.ReadLine () at the end of "main", without inserting a loop.
Now your concern is about stopping the application:
I also did this through some rudimentary “file” monitor. Simply creating the quit.txt file in the root folder of the application (either by my program or by another application that may require it to stop) will cause the application to shut down. Code:
<do your timer thing here> watcher = new FileSystemWatcher(); watcher.Path = <path of your application or other known accessible path>; watcher.Changed += new FileSystemEventHandler(OnNewFile); Console.ReadLine();
OnNewFile might be something like this:
private static void OnNewFile(object source, FileSystemEventArgs e) { if(System.IO.Path.GetFileName(e.FullPath)).ToLower()=="quit.txt") ... remove current quit.txt Environment.Exit(1); }
Now have you mentioned that this (or maybe) is for a mobile application? You may not have a file system watcher. In this case, perhaps you just need to “kill” the process (you said: “In special situations (for example: updating the application) I need to ask the application to stop.” Whoever “requesting” to stop it should just kill the process)
Nelson Rodriguez May 18 '12 at 20:01 2012-05-18 20:01
source share