Nickie Bryson

Nickie Bryson

  • NA
  • 6
  • 8.9k

C# Parallel foreach issue

Jan 23 2013 5:22 AM
Hi

Im getting an exception thrown in the md5 hash method that the file does not exist.  Im new to using parallel foreach but i would have thought that the fileexists would prevent a row from the data table getting inside the if that calls GetMd5Hash.

Brief outline of the general code functionality is i have a table in sql which contains paths to files.  Im looking to loop round each row get the md5 for each file and record it to the database.  If the file doesnt exist then record that to the db.  The code works fine and as expected using foreach (DataRow dr in ds.Tables[0].Rows).

Is there anything fundamentally im doing wrong below.

Thanks

Parallel.ForEach(ds.Tables[0].AsEnumerable(), dr =>
            {
                    sPath = dr["path"].ToString();

                    if (FileExists(sPath))
                    {
                        
                        //md5 hash the file
                        sFile_Hash = GetMd5Hash(sPath)
                        
                        //Update the database for the row
                        //Code removed
                        
                    }
                    else
                    {
                        //Record file does not exist
                        RecordFileNotExistsFailure(strConn, dr);
                    }
                }); 

Answers (2)