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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:04:04+00:00 2026-05-30T15:04:04+00:00

I have this piece of code i want to synchronized between the StartWatch() and

  • 0

I have this piece of code
i want to synchronized between the StartWatch() and OnFinalChanged() of the plugin so eventually what will be printed will be in the following order:

Creating plugin for file c:\temp\file1.txt
Creating plugin for file c:\temp\file2.txt
Creating plugin for file c:\temp\file3.txt
Starting watching c:\temp\file1.txt
Finished watching file c:\temp\file1.txt
Starting watching c:\temp\file2.txt
Finished watching file c:\temp\file2.txt
Starting watching c:\temp\file3.txt
Finished watching file c:\temp\file3.txt
namespace Test2
{
    internal static class MyProgram
    {
        [STAThread]
        private static void Main(string[] args)
        {
            List<Plugin> plugins = new List<Plugin>
                                       {
                                           new Plugin(@"c:\temp\file1.txt"),
                                           new Plugin(@"c:\temp\file2.txt"),
                                           new Plugin(@"c:\temp\file3.txt"),
                                       };

            foreach (var plugin in plugins)
            {
                // I need StartWatch to be called only after i got OnChanged() for the previous plugin
                plugin.StartWatch();
            }
        }
    }


    public class Plugin
    {
        private FileSystemWatcher _watcher;
        private readonly string _file;

        public Plugin(string file)
        {
            _file = file;
            Console.WriteLine("Creating plugin for file {0}", _file);
        }

        public void StartWatch()
        {
            Console.WriteLine("Starting watching {0}", _file);
            FileInfo fileInfo = new FileInfo(_file);
            if (fileInfo.Directory != null)
            {
                _watcher = new FileSystemWatcher(fileInfo.Directory.FullName, fileInfo.Name);
                _watcher.NotifyFilter = NotifyFilters.LastWrite;
                _watcher.Changed += OnChanged;
                _watcher.EnableRaisingEvents = true;
            }
            // i want the main thread to be paused untill i get the OnChanged()
        }

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("Finished watching file {0}", _file);
            _watcher.Dispose();

            // once i get this event, i want the main thread to continue and to start watching the same plugin
        }
    }
}
  • 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-30T15:04:05+00:00Added an answer on May 30, 2026 at 3:04 pm

    Using AutoResetEvent should do the job.

    namespace Test2
    {
        internal static class MyProgram
        {
            [STAThread]
            private static void Main(string[] args)
            {
                List<Plugin> plugins = new List<Plugin>
                                           {
                                               new Plugin(@"c:\temp\file1.txt"),
                                               new Plugin(@"c:\temp\file2.txt"),
                                               new Plugin(@"c:\temp\file3.txt"),
                                           };
    
                foreach (var plugin in plugins)
                {
                    // I need StartWatch to be called only after i got OnChanged() for the previous plugin
                    plugin.StartWatch();
                    plugin.WaitHandler.WaitOne();
                }
            }
        }
    
    
        public class Plugin
        {
            private FileSystemWatcher _watcher;
            private readonly string _file;
            public AutoResetEvent WaitHandler {get;private set;} 
    
            public Plugin(string file)
            {
                _file = file;
                Console.WriteLine("Creating plugin for file {0}", _file);
                WaitHandler = new AutoResetEvent(false);
            }
    
            public void StartWatch()
            {
                Console.WriteLine("Starting watching {0}", _file);
                WaitHandler.Reset();
                FileInfo fileInfo = new FileInfo(_file);
                if (fileInfo.Directory != null)
                {
                    _watcher = new FileSystemWatcher(fileInfo.Directory.FullName, fileInfo.Name);
                    _watcher.NotifyFilter = NotifyFilters.LastWrite;
                    _watcher.Changed += OnChanged;
                    _watcher.EnableRaisingEvents = true;
                }
                // i want the main thread to be paused untill i get the OnChanged()
            }
    
            private void OnChanged(object source, FileSystemEventArgs e)
            {
                Console.WriteLine("Finished watching file {0}", _file);
                _watcher.Dispose();
    
                // once i get this event, i want the main thread to continue and to start watching the same plugin
                WaitHandler.Set();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code in a function. I want to print the
I have this piece of code: var arrayParamaters = sParameters[split](|); I want to find
Say I have this piece of code, and I want to execute it on
I have this piece of code. I want to remove the rows created with
I have this HTML piece of code and I want to display:none on the
I still need your help. I have this piece of code that doesn't want
I have a class method where I want to perform this piece of code
I have this small piece of code and i want to to simulate the
I have this piece of code: public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener) {
I have this piece of code: @Entity @Table(name = MOVERS) public class MOVers implements

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.