GREG_DORIAN
New member
- Joined
- Aug 31, 2015
- Messages
- 4
- Programming Experience
- 1-3
I need to copy files from one directory to another, depending on the existence of the file name in a table of SQL database.
For this I use the following code:
then I use a an hashSet and Parallel.ForEach for copy in the fastest way an amount of 140.000 files depending if exist in the database
but in the parallel.ForEach does not copy anything? what is wrong w/ this code??
this is the first time I use parallel.Foreach..
For this I use the following code:
VB.NET:
[COLOR=#000000]Public Iterator Function ReadFileNamesFromDatabase() As IEnumerable(Of Integer)[/COLOR]
Try
Using connection As New SqlConnection("conection")
Using cmd As SqlCommand = connection.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT ID, PCOUNT FROM dbo.DocumentPics WHERE PCOUNT = 1"
connection.Open()
Using reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Yield reader.GetInt32(reader.GetOrdinal("ID"))
End While
End Using
connection.Close()
End Using
End Using
Catch ex As Exception
Throw ex [COLOR=#000000] End Try[/COLOR]
then I use a an hashSet and Parallel.ForEach for copy in the fastest way an amount of 140.000 files depending if exist in the database
VB.NET:
Sub Main() Dim source As New DirectoryInfo("C:\SourcePics\")
Dim destination As New DirectoryInfo("C:\destinationPics\")
Dim destinationPath As String
Dim filesToBeCopied As New HashSet(Of Integer)(ReadFileNamesFromDatabase())
Dim options As New ParallelOptions() With { _
.MaxDegreeOfParallelism = 4 _
}
Parallel.ForEach(filesToBeCopied.SelectMany(
Function(fn) source.EnumerateFiles(fn)), options, _
Sub(fi)
destinationPath = Path.Combine(destination.FullName, Path.ChangeExtension(fi.Name, ".tif"))
fi.CopyTo(destinationPath, False)
End Sub)
' Keep the console window open in debug mode.
Console.WriteLine("Processing complete. Press any key to exit.")
Console.ReadKey()
End Sub
but in the parallel.ForEach does not copy anything? what is wrong w/ this code??
this is the first time I use parallel.Foreach..