Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to display progress? #1

Open
Agagamand opened this issue Aug 8, 2019 · 0 comments
Open

How to display progress? #1

Agagamand opened this issue Aug 8, 2019 · 0 comments

Comments

@Agagamand
Copy link

I need to calculate checksums of large files with a progress bar. I use this code, but the progress bar fills up before I get the hash. How to get the correct progress?

private string GetYYP_XXHash(string filename, IProgress<double> progress = null)
{
    byte[] buffer;
    byte[] oldBuffer;
    int bytesRead;
    int oldBytesRead;
    long size;
    long totalBytesRead = 0;

    using (Stream stream = File.OpenRead(filename))
    using (YYProject.XXHash.XXHash64 hashAlgorithm = YYProject.XXHash.XXHash64.Create())
    {
        size = stream.Length;
        buffer = new byte[1048576]; // 1MB buffer

        bytesRead = stream.Read(buffer, 0, buffer.Length);
        totalBytesRead += bytesRead;

        do
        {
            oldBytesRead = bytesRead;
            oldBuffer = buffer;

            buffer = new byte[Convert.ToInt32(BufferHash)];
            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);
            }

            progress?.Report((double)totalBytesRead * 100 / size);

        } while (bytesRead != 0);


        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < hashAlgorithm.Hash.Length; i++)
        {
            sBuilder.Append(hashAlgorithm.Hash[i].ToString("x2"));
        }

        return sBuilder.ToString();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant