Search code examples
c#filesystemwatcher

filesystemwatcher multiple files


I have a doubt using FileSystemWatcher in C#.
I need to be notified when a file is created in a specified folder, but the problem is if I create multiple files at the same time multiple events are beeing fired and I need that the code only continues if the previous file was processed and not process multiple files at the same time.

Any clue how to do that?

Many thanks


Solution

  • pseudo-code:

    bool wasLastObjectProcessed = true
    
    function onFileWatcherCreateFile
        lock wasLastObjectProcessed
            if wasLastObjectProcessed and processFile(file)
                #do some code here that you need to do if it is processed
            else
               wasLastObjectProcessed = false
            endif
        endlock
    endfunction