How to show progress bar while encrypting file in C#

Discussion related to AES Crypt, the file encryption software for Windows, Linux, Mac, and Java.
Post Reply
imranshafqat
Posts: 1
Joined: Mon Sep 10, 2012 7:22 am

How to show progress bar while encrypting file in C#

Post by imranshafqat »

I am using AES Crypt in my C# program and I want to show a progress bar so that user can know how much work is done. I want to encrypt one file at a time so no need to handle progress dialogs for multiple files or I want it simple in other words.

I have tried but I have no idea on how to do this? Please help me this is important.

In the accompanied AES c++ gui program the programmer have created a progress bar and I liked it very much. I opened that c++ code and tried to figure out how it works but even after debugging the code line by line, I still don't understand how it is done in c++.

Can anyone give me a sample code for this problem?
User avatar
paulej
Posts: 593
Joined: Sun Aug 23, 2009 7:32 pm
Location: Research Triangle Park, NC, USA
Contact:

Re: How to show progress bar while encrypting file in C#

Post by paulej »

What I did in the C++ code was pause periodically and send a message to the dialog box containing the status bar. You'll see this code in the AESCryptWorkerThreads.cpp file:

Code: Select all

if ((clock() - last_clock_time >
    (CLOCKS_PER_SEC / 4)) ||
    (bytes_left == 0) ||
    (current_percent > last_percent))
{
    // Update the progress bar
    dlg.SendDlgItemMessage(
        IDC_PROGRESSBAR,
        PBM_SETPOS,
        (WPARAM) current_percent,
        0);
    dlg.UpdateWindow();
    DoMessageLoop();
    if (dlg.abort_processing == true)
    {
        error_abort = true;
        break;
    }

    last_clock_time = clock();
    last_percent = current_percent;
}
What this does is check if any of these conditions are true:
  • 250ms has passed since the last update
  • One or more percentage of the file has been decrypted
  • There are no bytes left to process (at 100%; probably redundant)
The code then updates the dialog box and allow Windows to process dialog messages. The reason for the latter is so the program does not appear to hog 100% of the machine and to allow a check to see if the user pressed the cancel button. I figured 250ms was a reasonable response time.

I hope that helps with working out an approach in C#
Post Reply