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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:31:20+00:00 2026-05-13T21:31:20+00:00

I am developing a database file system .It includes a multi-directory watcher which is

  • 0

I am developing a database file system.It includes a multi-directory watcher which is a windows service and which uses the file system watcher class from .net.

I want to run each watcher class on separate thread.Thread can not be extended in .net because as it is “Sealed”.
What I want is, run all the methods of my watcher class in the related thread.
How can I achieve this?

EDIT –

Following is my base watcher class.

 public abstract class WatcherBase
  {
    private IWatchObject _watchObject;

    public WatcherBase() { }

    public WatcherBase(IWatchObject watchObject, bool canPauseAndContinue)
    {
        _watchObject = watchObject;
        CanPauseAndContinue = canPauseAndContinue;
    }

    public bool CanPauseAndContinue { get; set; }

    public IWatchObject ObjectToWatch
    {
        get
        { 
            return _watchObject; 
        }
    }

    public abstract void Start();
    public abstract void Pause();
    public abstract void Continue();
    public abstract void Stop();
}

Following is my directory watcher class extended from the WatcherBase class

  namespace RankFs.WatcherService
  {
    public class DirectoryWatcher : WatcherBase
    {
    private WatchDirectory _directoryToWatch;
    private FileSystemWatcher _watcher;

    public DirectoryWatcher(WatchDirectory directory, bool CanPauseAndContinue)
        :base(directory ,CanPauseAndContinue)
    {
        _directoryToWatch = directory;
        _watcher = new FileSystemWatcher(_directoryToWatch.Path);
        _watcher.IncludeSubdirectories = _directoryToWatch.WatchSubDirectories;
        _watcher.Created +=new FileSystemEventHandler(Watcher_Created);
        //_watcher.Changed +=new FileSystemEventHandler(Watcher_Changed);
        _watcher.Deleted +=new FileSystemEventHandler(Watcher_Deleted);
        _watcher.Renamed +=new RenamedEventHandler(Watcher_Renamed);
    }

    public WatchDirectory DirectoryToWatch
    {
        get 
        {
            return _directoryToWatch;
        }
    }

    public override void Start()
    {
        _watcher.EnableRaisingEvents = true;
    }

    public override void Pause()
    {
        _watcher.EnableRaisingEvents = false;
    }

    public override void Continue()
    {
        _watcher.EnableRaisingEvents = true;
    }

    public override void Stop()
    {
        _watcher.EnableRaisingEvents = false;
    }

    private void Watcher_Created(object sender, FileSystemEventArgs e)
    {
       // adds a new file entry to database 
    }

    private void Watcher_Deleted(object sender, FileSystemEventArgs e)
    {
       //updates the database(deleted file)
    }

    private void Watcher_Renamed(object sender, RenamedEventArgs e)
    {
   //updates the database(renamed file)
    }
} }

I am stuck at this point.Please help me.

  • 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-13T21:31:21+00:00Added an answer on May 13, 2026 at 9:31 pm

    The FileSystemWatcher will trigger the events in a separate thread. The logic inside the event handlers will need to take that fact in consideration and perform any synchronization needed.

    If you run the following example you will see that the Changed event handler will run in a different thread.

    public static void Main(string[] args)
    {
        Directory.CreateDirectory("dir1");
        Directory.CreateDirectory("dir2");
        Directory.CreateDirectory("dir3");
    
        Console.WriteLine("Main Thread Id: {0}", 
            Thread.CurrentThread.ManagedThreadId);
    
        const int watcherCount = 3;
        string[] dirs = new string[] { "dir1", "dir2", "dir3" };
    
        for (int i = 0; i < watcherCount; i++)
        {
            var watcher = new FileSystemWatcher();
            watcher.Path = dirs[i];
            watcher.Changed += (sender, e) =>
            {
                Console.WriteLine("File: {0} | Thread: {1}", e.Name,
                    Thread.CurrentThread.ManagedThreadId);
    
                Thread.Sleep(2000); // Simulate long operation
            };
            watcher.EnableRaisingEvents = true;
        }
    
        File.WriteAllText(@"dir1\test1", "hello");
        File.WriteAllText(@"dir2\test2", "hello");
        File.WriteAllText(@"dir3\test3", "hello");
    
        Thread.Sleep(10000);
    }
    

    When running this sample I obtained the following output:

    // Main Thread Id: 1
    // File: test1 | Thread: 3
    // File: test2 | Thread: 4
    // File: test3 | Thread: 5
    // File: test1 | Thread: 3
    // File: test2 | Thread: 4
    // File: test3 | Thread: 5
    

    UPDATE:
    Since you are using the events approach in the FileSystemWatcher you are already using a multithreaded approach because the event handlers for each FileSystemWatcher instance will be triggered in a separate thread in an asynchronous way.

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

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The method explain in the Searcher is a nice way… May 14, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer You need one UIImageView for each image, but they will… May 14, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer For the field to be able to store unicode characters,… May 14, 2026 at 10:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.