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

  • Home
  • SEARCH
  • 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 1082091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:15:10+00:00 2026-05-16T22:15:10+00:00

I have a program that calls a Filewatcher function, like this Main() { watch();

  • 0

I have a program that calls a Filewatcher function, like this

Main()  
{  
    watch();        
    console.Readkey();
}

I have the console.readkey at the end because I don’t want the console to display ‘Press any key to continue’ while the file is being watched for changes. Now, if a change is detected, another function gets triggered off, which does its thing, and eventually calls main again, which once again stops at console.readkey.

At this point, I am experiencing some weird problems like the program requires two key inputs to continue. I’m guessing thats because the console.readkey from before is still waiting for an input, so the first input goes there, and then the second input goes to the second console.readkey.

So, my question is, the first time when the filewatcher gets triggered, can i, via code, feed something to the console.readkey, thats waiting for a user input?

  • 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-16T22:15:10+00:00Added an answer on May 16, 2026 at 10:15 pm

    Console.ReadKey will block the program until you press a key (at which point it will read that and return).

    It sounds like you, in this situation, need to change your logic to just loop indefinitely (or until some other condition is reached). Instead of using Console.ReadKey to prevent the application from ending, you should consider re-writing it like:

    bool exitProgram = false;
    AutoResetEvent resetEvent = new AutoResetEvent();
    
    void Main()
    {
        while (!exitProgram)
        {
            Watch(); // Starts FileSystemWatcher
            resetEvent.WaitOne();
        }
    }
    
    void WorkFinished() // Call at the end of FileSystemWatcher's event handler
    {
         resetEvent.Set(); // This "reschedules" the watcher...
    }
    

    This will make the program run “forever”, until you set exitProgram to true, at which point it will exit normally. The “watch” will not get called continually, since resetEvent will block indefinitely. When your “work” finishes (after the FileSystemWatcher event handler completes), call resetEvent.Set(). This will cause the loop to repeat one more time, re-triggering your watch code.

    It works by using an AutoResetEvent to prevent the watcher from “rewatching” the same file repeatedly.

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

Sidebar

Related Questions

I have a parent program that calls a worker console app when and update
I have a Python script that calls an executable program with various arguments (in
I have a program that I need to run under *nix and windows. because
I have a program that uses a Microsoft Access database for its back-end. I
I have a Java program that calls a C++ program to authenticate users. I
I have a program that makes some hefty calls to the database and then
I have a program that does recursive calls for 2 billion times and the
I have written a c# program that calls a c++ dll that echoes the
I have a program that makes calls to an internal web API. However when
I have a program that makes calls to an internal web API. However when

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.