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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:15:15+00:00 2026-05-13T16:15:15+00:00

I have a filesystemwatcher that will trigger an event when a file is modified.

  • 0

I have a filesystemwatcher that will trigger an event when a file is modified. I want to read from that file once the lock has been removed. At the moment I am just trying to open the file once the event is triggered, when A large file is being copied the file lock stays on for a while after the events have been sent, preventing the file from being opened for read access.

Any suggestions?

  • 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-13T16:15:15+00:00Added an answer on May 13, 2026 at 4:15 pm

    This one’s actually a bit of a doozie, unless the problem space has changed significantly since I last had to deal with it.

    The easiest way is to simply try to open the file, catch the resulting IOException, and if the file is locked, add it to a queue to be checked later. You can’t just try to process every file that comes in because there are all kinds of cases where multiple events will be generated for the same file, so setting up a retry loop on every single received event can turn into a disaster, fast. You need to queue them up instead and check the queue at a regular interval.

    Here is a basic class template that should help you out with this problem:

    public class FileMonitor : IDisposable
    {
        private const int PollInterval = 5000;
    
        private FileSystemWatcher watcher;
        private HashSet<string> filesToProcess = new HashSet<string>();
        private Timer fileTimer;  // System.Threading.Timer
    
        public FileMonitor(string path)
        {
            if (path == null)
                throw new ArgumentNullException("path");
    
            watcher = new FileSystemWatcher();
            watcher.Path = path;
            watcher.NotifyFilter = NotifyFilters.FileName;
            watcher.Created += new FileSystemEventHandler(FileCreated);
            watcher.EnableRaisingEvents = true;
    
            fileTimer = new Timer(new TimerCallback(ProcessFilesTimer),
                null, PollInterval, Timeout.Infinite);
        }
    
        public void Dispose()
        {
            fileTimer.Dispose();
            watcher.Dispose();
        }
    
        private void FileCreated(object source, FileSystemEventArgs e)
        {
            lock (filesToProcess)
            {
                filesToProcess.Add(e.FullPath);
            }
        }
    
        private void ProcessFile(FileStream fs)
        {
            // Your code here...
        }
    
        private void ProcessFilesTimer(object state)
        {
            string[] currentFiles;
            lock (filesToProcess)
            {
                currentFiles = filesToProcess.ToArray();
            }
            foreach (string fileName in currentFiles)
            {
                TryProcessFile(fileName);
            }
            fileTimer.Change(PollInterval, Timeout.Infinite);
        }
    
        private void TryProcessFile(string fileName)
        {
            FileStream fs = null;
            try
            {
                FileInfo fi = new FileInfo(fileName);
                fs = fi.OpenRead();
            }
            catch (IOException)
            {
                // Possibly log this error
                return;
            }
    
            using (fs)
            {
                ProcessFile(fs);
            }
    
            lock (filesToProcess)
            {
                filesToProcess.Remove(fileName);
            }
        }
    }
    

    (Note – I’m recalling this from memory here so it might not be perfect – let me know if it’s buggy.)

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

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • 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 On the after_save callback, just check the attribute and destroy… May 15, 2026 at 4:46 am
  • Editorial Team
    Editorial Team added an answer Displaying Text in the Browser's Status Bar When Mousing Over… May 15, 2026 at 4:46 am
  • Editorial Team
    Editorial Team added an answer You cannot send window messages to a service. For one,… May 15, 2026 at 4:46 am

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.