Cinchoo – ChoConsolePercentageProgressorEx

Cinchoo framework provides another elegant console progress bar indicator to perform lengthy operation in the console application. The feature is very useful when you want to give a visual indicator of how a task is performing.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core.Shell.Console

Below is the sample code

using (ChoConsolePercentageProgressorEx cp = new ChoConsolePercentageProgressorEx("Calculating..."))
{
    cp.ErrorOccured += (sender, eventArg) => cp.Write(eventArg.Exception.Message);

    cp.Start((sender, runningPercentage, state) =>
    {
        Thread.Sleep(1000);
        if (runningPercentage == cp.MinPercentage)
            return new Tuple<int, string>(10, "Step 1 - Status Msg");

        switch (runningPercentage)
        {
            case 10:
                //Do the custom operation here
                return new Tuple<int, string>(25, "Step 2 - Status Msg");
            case 25:
                //Do the custom operation here
                return new Tuple<int, string>(90, "Step 3 - Status Msg");
            default:
                //Do the custom operation here
                return new Tuple<int, string>(cp.MaxPercentage, "Complete");
        }
    }
    );
}

When you run the above code, you can see the below output

For any exception occurred during the operation, can be captured by subscribing to ErrorOccured event.

cp.ErrorOccured += (sender, eventArg) => Console.WriteLine(eventArg.Exception.ToString());

The custom operation can be terminated in couple of ways

1. Stop() – Graceful termination

2. Abort() – Abnormal termination

Happy coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s