示例程序:在某个文件夹上侦听 FileSystem 事件,并在 Timer 事件触发时将 FileSystem 事件信息打印到控制台。
class Program
{
public static string location = @"D:\TestEvents";
public static double interval = 15000;
public static System.Timers.Timer timer;
public static List<string> listOfChanges = new List<string>();
static void Main(string[] args)
{
StartWatch();
StartTimer();
Console.ReadLine();
}
private static void StartWatch()
{
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Path = location;
Watcher.Created += new FileSystemEventHandler(OnFileCreatedOrDeleted);
Watcher.Deleted += new FileSystemEventHandler(OnFileCreatedOrDeleted);
Watcher.EnableRaisingEvents = true;
}
static void OnFileCreatedOrDeleted(object sender, FileSystemEventArgs e)
{
listOfChanges.Add(String.Format("Change Type: {0}, Name: {1}, Time: {2}", e.ChangeType, e.Name, DateTime.Now));
}
private static void StartTimer()
{
timer = new System.Timers.Timer();
timer.AutoReset = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimerEpleased);
timer.Interval = interval;
timer.Start();
}
private static void OnTimerEpleased(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Timer event fired: " + DateTime.Now);
foreach (var item in listOfChanges)
{
Console.WriteLine(item);
}
Console.WriteLine();
listOfChanges.Clear();
timer.Interval = interval;
timer.Start();
}
}
List<string> listOfChanges
从两个事件处理程序访问相同的存储静态是否安全?我真的不明白事件是如何在下面工作的。它是否创建了一些全局事件处理程序队列并一一运行所有事件处理程序,尽管事件类型如何?或者它为每种事件处理程序类型创建不同的线程?
编辑:
我想最好的解决方案是使用BlockingCollection
with ConcurrentQueue
,所以应该是这样的:
public static BlockingCollection<string> listOfChanges = new BlockingCollection<string>();
static void OnFileCreatedOrDeleted(object sender, FileSystemEventArgs e)
{
listOfChanges.Add(String.Format("Change Type: {0}, Name: {1}, Time: {2}", e.ChangeType, e.Name, DateTime.Now));
}
private static void OnTimerEpleased(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Timer event fired: " + DateTime.Now);
while (listOfChanges.Count > 0)
{
string item;
bool b = listOfChanges.TryTake(out item);
if (b)
{
Console.WriteLine(item);
}
}
Console.WriteLine();
timer.Interval = interval;
timer.Start();
}