Give instant generation feedback with the ProgressBar

One of the easiest ways to show instant template feedback for long running templates is by using the Progress Bar that the CodeSmith Engine API provides. If you’ve ever used the Progress control in Winforms you’ll find this control very similar in usage and very easy to use.

The progress bar is meant to be a way to show a series of steps during the generation process. This lets the user know how long the generation process will take relative to the steps which have already been completed. To access and configure the progress bar, there is a property exposed from the CodeTemplate class called Progress, so you will easily be able to apply this to any new or existing template. Typing this.Progress from anywhere in your templates will give you access to configure the Progress class.

The important properties and methods from the Progress class are:

  • MaximumValue = The maximum value that the series can step to. Once the steps have counted to this value the progress bar will be full.
  • Step = This is the increment value you want to perform each step. This is most commonly 1, since each step should be for a unit of work.
  • Value = This is the current value of the progress bar.
  • PerformStep() = This method triggers a step to be processed.

Example:

Let’s say we want to run through all of the tables in the Database and for this example, we’re going to generate against the table’s meta-data information.

Steps

1. OnInit of the template Setup the Progress Bar
2. Progress’ MaximumValue with the amount of tables found in the database.
3. Set the Step = 1, since we will be processing each table individually.
4. During the generation process, after each unit of work, call PerformStep().

Result:

The result is that as the generation progress is occurring from CodeSmith Studio or Windows Explorer, the progress bar will be shown to give instant feedback to the user.