简体   繁体   中英

Parallel processing of Directory

I am working on File directory monitoring in which i have to look for a specific directory "D:\\Watch" and if there is any new file detects i have to process it and show the contents. I was using FileSystemWatcher but my requirement is to check directory for every 1 minute.That's why i skip the FileSystemWatcher and make a Windows service which is schedule after every 1 minute and read the directory to process the new files.

But my requirement is to read the files in Parallel and show the contents after the file is processed and don't need to wait for other files to be processed. I also need to handle the exception if any of the file has not proper contents.

How i can achieve this task with above requirements in parallel. I am using C#.net. Thanks

If I understood you correctly, this code should work for you.

  var filePaths = Directory.EnumerateFiles(@"c:\mydir");
  Parallel.ForEach(filePaths, ProcessFile);

Here is your processing logic

  private void ProcessFile(string filePath)
  {
      //do what you have to do here. Proper exception handling is needed in this method      
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM