Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            byte[] buffer;
            byte[] oldBuffer;
            int bytesRead;
            int oldBytesRead;
            long size;
            long totalBytesRead = 0;

            using (Stream stream = File.OpenRead((string)e.Argument))
            using (HashAlgorithm hashAlgorithm = SHA1.Create())//MD5.Create())
            {
                size = stream.Length;
                buffer = new byte[4096];
                bytesRead = stream.Read(buffer, 0, buffer.Length);
                totalBytesRead += bytesRead;
                do
                {
                    oldBytesRead = bytesRead;
                    oldBuffer = buffer;

                    buffer = new byte[4096];
                    bytesRead = stream.Read(buffer, 0, buffer.Length);

                    totalBytesRead += bytesRead;

                    if (bytesRead == 0)
                    {
                        hashAlgorithm.TransformFinalBlock(oldBuffer, 0, oldBytesRead);
                    }
                    else
                    {
                        hashAlgorithm.TransformBlock(oldBuffer, 0, oldBytesRead, oldBuffer, 0);
                    }

                    BackgroundWorker.ReportProgress((int)((double)totalBytesRead * 100 / size));
                } while (bytesRead != 0);
                e.Result = hashAlgorithm.Hash;


Having multiples of the above but to use between md5, sha1, sha256 and so on?
Posted
Comments
Sergey Alexandrovich Kryukov 13-Jun-15 3:11am    
It does not make any sense at all. Just think about it. Think what is a thread, what's it's purpose. Are you getting the hint?
—SA

1 solution

Can you have multiple DoWork methods? No.

I think the way to fix this is rather obvious. Use multiple BackgroundWorkers, each with it's own DoWork method.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jun-15 3:11am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900