Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:51:43+00:00 2026-06-15T12:51:43+00:00

Example program: Listen FileSystem events on some folder and print FileSystem events info to

  • 0

Example program: Listen FileSystem events on some folder and print FileSystem events info to console when Timer event fires.

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

Is it safe to access the same storage static List<string> listOfChanges from both event handlers?
I don’t really understand how events works underneath. Is it creates some global event handler queue and runs all event handlers one-by-one despite of event type? Or it creates different threads for each event handler type?

Edit:
I guess the best solution is to use BlockingCollection with ConcurrentQueue, so it should be like this:

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:51:44+00:00Added an answer on June 15, 2026 at 12:51 pm

    A generic List is not thread safe. It will be possible that you will get an error when listOfChanges is traversed (in OnTimerEpleased), and a new entry would have added an entry to the list (OnFileCreatedOrDeleted). See http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx. Either you synchronize access to the list or use a built-in thread safe collection: http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the example with comments: class Program { // first version of structure
Possible Duplicate: How to listen to keyboard events in a console application Hi I
I want to fork a sub-program to do some routine jobs or listen to
Consider the following example program: next :: Int -> Int next i | 0
Could anyone give an example program that explains Java Threads in a simple way?
What is an example program that has a bug due to _ReadBarrier() not being
I'm trying to compile an example program that links to the shared library produced
I've created this simple example program, but this doesn't work. it won't connect. any
I'm trying to run the example program for POSIX message queues found in the
For Example: I need to develop a Small Inventory program for nearby Super Market

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.