0

I want to calculate few different hashes of a file in parallel in order to increase the performance of my application.

I have the following code:

Dim lThreads As List(Of Thread) = New List(Of Thread)()

For i = 0 To algorithms.Count - 1
    Dim index = i

    Dim t As Thread = New Thread(
        New ThreadStart(Sub()
            Dim reader2 as System.IO.FileStream = reader
            reader2.position = 0
            listDigestvalue(index) = CalculateHash(algorithm, listReader(index))
        End Sub))
    t.Name = "Thread of algorithm: " + shortnameofalgorithm
    t.Start()
    lThreads.Add(t)
Next

For i = 0 To lThreads.Count - 1
    lThreads(i).Join()
Next

The problem is that for each execution, program doesn´t generate the correct hash of the file.

Is there another way to paralellize these operations.

Thank you in advance.

  • 2
    You should not be using a single `FileStream`. Either create a different `FileStream` for each hash or else read the file contents into a `Byte` array and use that each time. – jmcilhinney May 14 '20 at 11:30
  • @jmcilhinney Thank you very much, with your aproximation the code works perfectly although I think it´s a little bit slower generate the hash with a byte array than using the reader, – Miguel Sanchez May 18 '20 at 06:03
  • If possible, you should post an answer showing your solution and accept it, to help others and show that you no longer need help. I'm not 100% sure whether you need more reputation points first though. – jmcilhinney May 18 '20 at 06:27

1 Answers1

0

Thanking @jmcilhinney, I finally solve my problem with this code.

Dim reader = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, hashBufferSize)
Dim bytesFile() As Byte = GetStreamAsByteArray(reader)

Dim lTasks As List(Of Task) = New List(Of Task)()

For i = 0 To algorithms.Count - 1
    Dim index = i
    Dim shortnameofalgorithm = algorithms(i).shortname

    Dim t As Task = Task.Run(Sub()
                                 listDigestvalue(index) = CalculateHash(shortnameofalgorithm, bytesFile)
                             End Sub)
    lTasks.Add(t)
Next

Task.WaitAll(lTasks.ToArray)

For j = 0 To lTasks.Count - 1
    algorithms(j).hashValue = listDigestvalue(j)
Next

It´s possible to do the same with thread but I select "Tasks" because it improve the performance of my application.

Thank you very much.