2012-12-06 47 views
2

示例程序:偵聽某個文件夾中的FileSystem事件,並在Timer事件觸發時將FileSystem事件信息打印到控制檯。C#事件和線程

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是否安全? 我不太瞭解事件如何在底下工作。是否創建了一些全局事件處理程序隊列,並且儘管事件類型是一個接一個地運行所有事件處理程序?或者它爲每個事件處理程序類型創建不同的線程?

編輯: 我想最好的解決辦法是使用BlockingCollectionConcurrentQueue,所以它應該是這樣的:

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(); 
} 

回答

0

林不知道如果FileSystemWatcher對象使用一個以上的線程,但只是要你訪問列表中

lock (listOfChanges) 
{ 
    //Code that reads or writes to the listOfChanges. 
} 
安全包
0

你不應該假設,那些事件處理程序將從單個線程中調用。 FileSystemWatcher和Timer的文檔都沒有提到這些處理程序是如何被調用的,所以我會在這裏選擇安全的方式,並確保我自己對這個列表的訪問是同步的。