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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:54:54+00:00 2026-05-31T15:54:54+00:00

I have this code that is working fine for me: using System; using System.Collections.Generic;

  • 0

I have this code that is working fine for me:

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

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

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

The result that is printed from the code is the following (which is working fine and is important):

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

The problem i have is that i want to make my program multi-threaded and have another thread running in the background. i cannot do it with this design because the main thread is blocked by the AutoResetEvent.
The other thread that i want to add is a thread which will listen to a file named “AbortFlagFile.txt”. once “AbortFlagFile.txt” is modified than i want my program to be aborted.
I was thinking maybe to create a PluginEndEvent which will be fired after OnChange() is finished.

It is very important for me that the program will produce the logs in the same order as i wrote above
Thanks!

  • 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-31T15:54:55+00:00Added an answer on May 31, 2026 at 3:54 pm

    Declare an array of waithandles and a counter

    private static WaitHandle[] waithandles;
    private static int waitcount;
    

    In constructor, initialize it

    waithandles = new WaitHandle[3] { new AutoResetEvent(false), new AutoResetEvent(false) };
    

    you will also need a function to increment the thread counter

    private static void incThreadCounter()
    {
            lock (sobj)
        {
        waitcount++;
        }
    }
    

    in the code where you initialize your thread, call the incThreadCounter()

    I actually do mine as a callback after the thread has been spooled up. Prevents false counts

    in the area where you have your autoreset set a while loop, and decrement the thread count
    as they signal

    while (waitcount > 0)
    {
        WaitHandle.WaitAny(waithandles, 5000);
        waitcount--;
    }
    

    When the count is 0, you proceed to the next part, which may be spooling up additional threads

    If you want to make sure thread 2 runs after thread 1, you will want to use a variation of the logic above. In fact, if thread 2 always needs to run after thread 1 and thread 3 always needs to run after thread 2, then you don’t really need multi-threading. Threading is only valuable if there are processes that can run simultaneously.

    Hope this helps

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

Sidebar

Related Questions

I have this legacy code that I am working with and there is code
Ok, I have code that is working (posted after this) but before I expand
I have this code that fetches some text from a page using BeautifulSoup soup=
I have the following code that works fine on my localhost using IIS7 but
I have this code that performs an ajax call and loads the results into
I have this code that works in a unit test but doesn't work when
I have this code that I want to make point-free; (\k t -> chr
I have this code that changes the opacity of the div on hover. $(#navigationcontainer).fadeTo(slow,0.6);
I have this code that has one button that let's me choose an entry
I have this code that saves a pdf file. FileStream fs = new FileStream(SaveLocation,

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.