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 4598608
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:25:23+00:00 2026-05-21T23:25:23+00:00

I need to create a method for listening to events and waiting for a

  • 0

I need to create a method for listening to events and waiting for a certain amount of silence before calling another function.

Specifically, I am listening to a directory for file updates. When a file change occurs, my “directoryUpdate” function is called. From there I add the file to a list and create a thread called “timerThread” which sleeps for 2 seconds. When that thread is done sleeping, it calls a “gatherFinished” function.

But since directoryUpdate gets called 10 times all at once if 10 files change, it would launch 10 threads which seems like a bad way of doing this.

In the end I want a list of the file changes that occurred within 2 seconds of each other. I figure if there is a way to reset the sleep time to 2 seconds every time a change occurs and wait for the 2 seconds to finish, then I will have what I need. But there is no way to reset the timer as far as I know.

What is the best way of doing this?

UPDATE
Thanks for all your great answers. I am sorry for putting emphasis on getting the list of files. The event (file change) should not matter. I meant to focus on making sure the call to “gatherFinished” happens once at the right time – 2 seconds after all events in question have stopped firing. My question is on the nature of waiting, not on the nature of files or collections.

  • 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-05-21T23:25:24+00:00Added an answer on May 21, 2026 at 11:25 pm

    You could just use an AutoResetEvent and wait 2 seconds on it. If the event is triggered then you loop and wait another 2 seconds.

            AutoResetEvent resetTimer = new AutoResetEvent(false);
    
            ...
    
            private void TimerJob()
            {
                ...
    
                // The thread will sleep and loop until 
                // 2 seconds elapse without directory changes 
                while (resetTimer.WaitOne(2000)) {}
    
                ...
            }
    
            private void ResetTimer()
            {
                resetTimer.Set();
            }
    

    NOTE: I didn’t put any code to specify you how to synchronize the thread that will receive the directory changes and the timer thread. You will have to do that youself.

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

Sidebar

Related Questions

I need to create an Objective-C method that converts an int into a byte
This is a design question: when do I need to create/use a static method
I need some JS class or CSS method to create from any text such
I need to implement the classic Factory Method pattern in ASP.NET to create server
I need to create a method largerThan(See below) which takes a Rectangle object as
I need to create a method for my intranet web application that will authenticate
Basically, I need to create a method that will do its darn best to
I've got the following problem; I need to create a method, which generates a
Everything is working up to here, I just need to create the method that
I need to create an method invoker that any thread (Thread B for example

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.