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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:04:48+00:00 2026-05-19T04:04:48+00:00

I have a program that runs as a Windows Service which is processing files

  • 0

I have a program that runs as a Windows Service which is processing files in a specific folder.

Since it’s a service, it constantly monitors a folder for new files that have been added. Part of the program’s job is to perform comparisons of files in the target folder and flag non-matching files.

What I would like to do is to detect a running copy operation and when it is completed, so that a file is not getting prematurely flagged if it’s matching file has not been copied over to the target folder yet.

What I was thinking of doing was using the FileSystemWatcher to watch the target folder and see if a copy operation is occurring. If there is, I put my program’s main thread to sleep until the copy operation has completed, then proceed to perform the operation on the folder like normal.

I just wanted to get some insight on this approach and see if it is valid. If anyone else has any other unique approaches to this problem, it would be greatly appreciated.

UPDATE:

I apologize for the confusion, when I say target directory, I mean the source folder containing all the files I want to process. A part of the function of my program is to copy the directory structure of the source directory to a destination directory and copy all valid files to that destination directory, preserving the directory structure of the original source directory, i.e. a user may copy folders containing files to the source directory. I want to prevent errors by ensuring that if a new set of folders containing more subfolders and files is copied to the source directory for processing, my program will not start operating on the target directory until the copy process has completed.

  • 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-19T04:04:48+00:00Added an answer on May 19, 2026 at 4:04 am

    What you are looking for is a typical producer/consumer scenario. What you need to do is outlined in ‘Producer/consumer queue’ section on this page. This will allow you to use multi threading (maybe span a backgroundworker) to copy files so you don’t block the main service thread from listening to system events & you can perform more meaningful tasks there – like checking for new files & updating the queue. So on main thread do check for new files on background threads perform the actual coping task. From personal experience (have implemented this tasks) there is not too much performance gain from this approach unless you are running on multiple CPU machine but the process is very clean & smooth + the code is logically separated nicely.

    In short, what you have to do is have an object like the following:

    public class File
    {
        public string FullPath {get; internal set;}
        public bool CopyInProgress {get; set;} // property to make sure 
        // .. other properties if desired
    }
    

    Then following the tutorial posted above issue a lock on the File object & the queue to update it & copy it. Using this approach you can use this type approaches instead of constantly monitoring for file copy completion.
    The important point to realize here is that your service has only one instance of File object per actual physical file – just make sure you (1)lock your queue when adding & removing & (2) lock the actual File object when initializing an update.

    EDIT: Above where I say “there is not too much performance gain from this approach unless” I refere to if you do this approach in a single thread compare to @Jason’s suggesting this approach must be noticeably faster due to @Jason’s solution performing very expensive IO operations which will fail on most cases. This I haven’t tested but I’m quite sure as my approach does not require IO operations open(once only), stream(once only) & close file(once only). @Jason approach suggests multiple open,open,open,open operations which will all fail except the last one.

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

Sidebar

Related Questions

I have a cross platform program that runs on Windows, Linux and Macintosh. My
I have a program that runs osql.exe from microsoft sql server tools directory and
I have a Visual Studio 2005 C++ program that runs differently in Release mode
I have a PHP script that runs as a CGI program and the HTTP
I have a bash script that runs a simulation program written in Fortran 90,
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program that monitors debug messages and I have tried using a
I have been working on many applications which run as windows service or scheduled
I have written a pretty simple Windows service in C# that starts up automatically
With some slower machines that our application runs on (which is in the windows

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.