Sunday, April 22, 2012

ProgressBar example - cracked easy

Its been sometime that I am working with ProgressBar. Initially its been challenging to implement it; how to manage the process as well as progress bar simultaneously. Now a days I prefer using AsyncOperationManager for having the progress bar in a different thread. It has very less overhead and stable. There are many implementation example and I think all are fine but I was looking for something very portable and with minor modification can be integrated in any application, even with minor changes can be used in same application is different flavors.

In the example below created new form with ProgressBar which I have used for all the processes.
To make the implementation simple kept the main process in the main form thread e.g. copy files and folders; launched the ProgressBar in a separate process which basically puts the main form in the background. Even though progress bar form thread doesn't do anything in my case it is on the user discretion to use it.

In my case I have user Marquee progress bar type. This example has 3 section and details given below.

Declare the delegares as give below:

private delegate void AppDeligate();
private AppDeligate _psudoShowProgress;
private delegate void CloseDeligate();
private CloseDeligate _psudoCloseDialog;

Launch the progress bar form using Async process and invoke it. 

private void CopyShowProgressBar()
    ProcessBar PPForm = new ProcessBar ();
private void PsudoProcessBarLaunch()
    _psudoShowProgress = CopyShowProgressBar;
    var async = AsyncOperationManager.CreateOperation(null);
    _psudoShowProgress.BeginInvoke(null, async);

Once the main process completed then close the progress bar which is running on the foreground.

private void PsudoCloseDlg()
    ProcessBar Main = (ProcessBar)Application.OpenForms["ProcessBar"];
    if (Main != null)
private void PsudoCompletedCallback(IAsyncResult ar)
    if (ar.IsCompleted)
private void PsudoProcessBarClose()
    _psudoCloseDialog = PsudoCloseDlg;
    var asyncClose = AsyncOperationManager.CreateOperation(null);
    _psudoCloseDialog.BeginInvoke(PsudoCompletedCallback, asyncClose);

1 comment: