using robocopy with C# Foreach.Parallel

0

I have information about Source and destination dirs for multiple locations in Datatable.

I am iterating through the datatable using foreach parallel and then two tasks are performed, Copy files to dest and rename copied file on Dest.

While doing this sometimes i am getting errors like,
ERROR 32 (0x00000020) Accessing Source Directory The process cannot access the file because it is being used by another process. ,

ERROR 32 (0x00000020) Accessing Destination Directory The process cannot access the file because it is being used by another process.

And while renaming i am getting error as Error Renaming file, The process cannot access the file because it is being used by another process.

E.g.

   var options = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 4
                };
    Parallel.foreach(datatableObj.AsEnumerable(), options, row =>{

    string sourcePath = row.Field<string>("source_path");
    string destPath = row.Field<string>("dest_path");
    string searchPattern = row.Field<string>("Search_pattern");

    List<string> files=Directory.GetFiles(sourcePath, searchPattern);
    string ROBOCOPY_STATIC_ARGUMENTS = @"/MT:32 /BYTES /COPY:DAT /NP /R:3 /W:1";
    foreach(string file in fIles)
    {
          CopyUsingRoboCopy(file,DestPath,ROBOCOPY_STATIC_ARGUMENTS);
          RenameFile(file,NewFileName+datetimewithmilliseconds.ext);
    }

});

As 2 threads as executing in parallel manner but single threads execution is sequential i.e. First copy and then renameFiles. So i am not getting why i am getting errors mentioned above.

Note: Destination paths is same for all

c#
multithreading
robocopy
asked on Stack Overflow Feb 20, 2020 by Darshan Mestry • edited Feb 20, 2020 by Darshan Mestry

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0