Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, April 6, 2011 7:58 PM
I originally was trying to create a windows service (to watch for a file change (add) ) but was unable to do to company restirctions.? So i got the idea to start a console app with a scheduled task and just wait for the user to end it (while in testing)? Well, i am at the point where that part of testing is complete and now need to have the app stop itself after the onchange event fires one time here is the code:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\testWatcher\";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.xls*";
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
try
{
ImportPVC.ProcessDailyTx();
//After this fires once exit program
}
catch //(Exception ex)
{ }
}
All replies (5)
Wednesday, April 6, 2011 8:36 PM ✅Answered
Console.Read() blocks the STAThread on which the console app is running. The only thing that code will do is block until the user presses 'q'.
Wednesday, April 6, 2011 10:57 PM ✅Answered
The following should work. It will quit if the user hits Q or if the watcher.Created event gets fired. Hopefully it makes sense to you but if not please let me know.
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
bool Changed = false;
using (FileSystemWatcher watcher = new FileSystemWatcher(@"C:\Users\", "*.xls"))
{
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Created += delegate(object source, FileSystemEventArgs e)
{
// do whatever you want here...set Changed to true if you want the app to exit
Changed = true;
};
Console.WriteLine("Press \'Q\' to quit the sample.");
ConsoleKeyInfo cki;
while (!Changed && (!Console.KeyAvailable || (cki = Console.ReadKey(true)).Key != ConsoleKey.Q))
{
Thread.Sleep(1); // give up a timeslice to avoid 100% CPU usage while sleeping
};
}
}
HTH
ShaneB
Friday, April 8, 2011 9:06 AM ✅Answered | 1 vote
Hi WIJ,
If you expect the application stop itself after the onchange event fires one time, you can call the exit method in the OnChanged method like this:
private static void OnChanged(object source, FileSystemEventArgs e)
{
try
{
ImportPVC.ProcessDailyTx();
Environment.Exit(0);
}
catch //(Exception ex)
{ }
}
Wednesday, April 6, 2011 8:02 PM
Doesn`t this code:
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
do what you want?
It leaves the while loop when the user enters "q".
Wheres the problem?
Mitja
Tuesday, April 12, 2011 8:37 PM
ConsoleKeyInfo cki;
cki = Console.ReadKey(true);
while (hidden.KeyChar != 'q')
{
//do something...
//and than wait for user input,,,
cki = Console.ReadKey(true);
}
no need to treath.sleeping, this just wait for the user to press any key (and if key is q, quit!)
jeo suprima