Best Way To Display Time Progress [TimeProgressBar WinForms Control]

If you create a WinForms application and wish to display time progress, you can use the TimeProgressBar custom WinForms control instead of ProgressBar. For example, you may want to display the progress of a task and you know how much time it will take. While you can use ProgressBar control for this task, you probably want this control to display . TimeProgressBar displays more information about the task’s progress. You can see the time passed since the task starts, the remaining time of the task, and the percent of the time passed from the total time of the task.

How To Display Time Progress With TimeProgressBar?

The control displays a progress bar. This bar is colored with TimeMainColor and represents the total time of the task. The progress is indicated by an indicator rectangle which is colored with TimePassColor. This rectangle is overlapped with the progress bar and located on its left side. The size of this rectangle will be determined by the percent of the passed time from the total time of the task (i.e.: if 60% of the task has passed, the indicator rectangle width will be 60% of the width of the main rectangle).

The control also displays three properties about the progress of the task:

  • In the left, we can see the time passed since the start of the task,
  • In the center, we can see the percent of passed time from the total time of the task
  • In the right, the remaining time to the end of the task.

To start to monitor the time progress, we can use the Start methods.

  • The Start(TimeSpan duration) method will monitor the time progress of the task from the time of its invocation until the duration time has passed.
  • The Start(DateTime startTime,DateTime endTime) method will display progress of a task that start in startTime and ends in endTime.

There are 3 events raised by TimeProgressBar:

  • The OnStart event is raised when the task has started ,
  • The OnTick event is raised when the task has not ended yet and control has been updated. If AutoUpdate is true this event is raised each second.
  • The OnEnd event is raised when the task has ended

WinForms Demo of TimeProgressBar

WinForms Demo To Display Time Progress With AutoUpdate

In this demo we use the custom windows control to display time progress. We will use the default property values; that is, AutoUpdate is true.

Display Time Progress With AutoUpdate
Display Time Progress With AutoUpdate

When the form is loaded, we will connect the event handlers and set the task time to 20 seconds :

The OnLoad Method With AutoUpdate
 //-------------------------------------------------------------------------
 protected override void OnLoad(EventArgs e) {
     base.OnLoad(e);
     winTimeProgresBar.OnStart += new EventHandler(this.winTimeProgresBar_OnStart);
     winTimeProgresBar.OnTick  += new EventHandler(this.winTimeProgresBar_OnTick);
     winTimeProgresBar.OnEnd   += new EventHandler(this.winTimeProgresBar_OnEnd);

     winTimeProgresBar.Start(new TimeSpan(0,0,20));
 }

The handlers will display the appreciate message about the event. Please note that Invoke is used as the handlers will not be called in the GUI thread.

The Handlers
 //-------------------------------------------------------------------------
 private void winTimeProgresBar_OnStart(object sender,EventArgs e) {
     Invoke(new AppendTextProc(AppendText),"OnStart");
 }
 //-------------------------------------------------------------------------
 private void winTimeProgresBar_OnEnd(object sender,EventArgs e) {
     Invoke(new AppendTextProc(AppendText), "OnEnd");
 }
 //-------------------------------------------------------------------------
 private void winTimeProgresBar_OnTick(object sender,EventArgs e) {
     Invoke(new AppendTextProc(AppendText),"OnTick");
 }
 //-------------------------------------------------------------------------
 private delegate void AppendTextProc(string msg);
 private void AppendText(string prefix) {
     string ss = string.Format("{0,-8} : {1:D2}%

,prefix,winTimeProgresBar.TimePassPercent);
     winText.AppendText(ss);
 }
 //-------------------------------------------------------------------------

WinForms Demo To Display Time Progress Without AutoUpdate

While setting AutoUpdate to true is convenient, sometimes you may want that another source will be responsible for updating the control. In this case, we should set AutoUpdate to false. In this demo, we will create a new timer the will responsible for updating the control.

Display Time Progress Without AutoUpdate
Display Time Progress Without AutoUpdate
The OnLoad Method Without AutoUpdate
 //-------------------------------------------------------------------------
 protected override void OnLoad(EventArgs e) {
     base.OnLoad(e);
     winTimeProgresBar.OnStart += new System.EventHandler(this.winTimeProgresBar_OnStart);
     winTimeProgresBar.OnTick  += new System.EventHandler(this.winTimeProgresBar_OnTick);
     winTimeProgresBar.OnEnd   += new System.EventHandler(this.winTimeProgresBar_OnEnd);
     winTimeProgresBar.AutoUpdate = false;
     winTimeProgresBar.Start(new TimeSpan(0,0,20));
 }

The handlers will display the appreciate message about the event. The OnStart handler will create the timer. The timer will be destroyed by OnEnd handler.

The Handlers
 //-------------------------------------------------------------------------
 private void winTimeProgresBar_OnStart(object sender,EventArgs e) {
     Invoke(new AppendTextProc(AppendText),"OnStart");
     _timer = new System.Threading.Timer(
         _ => winTimeProgresBar.Update() , null, new TimeSpan(),new TimeSpan(0,0,1)
     );
 }
 //-------------------------------------------------------------------------
 private void winTimeProgresBar_OnEnd(object sender,EventArgs e) {
     Invoke(new AppendTextProc(AppendText), "OnEnd");
     _timer.Dispose();
 }
 //-------------------------------------------------------------------------
 private void winTimeProgresBar_OnTick(object sender,EventArgs e) {
     Invoke(new AppendTextProc(AppendText),"OnTick");
 }
 //-------------------------------------------------------------------------
 private delegate void AppendTextProc(string msg);
 private void AppendText(string prefix) {
     string ss = string.Format("{0,-8} : {1:D2}%

,prefix,winTimeProgresBar.TimePassPercent);
     winText.AppendText(ss);
 }
 //-----------------------------------------------------------

TimeProgressBar Summary – Custom WinForms Control

We see how can we create TimeProgressBar – a WinForms custom control to display time progress. one enum value. We also create a 2 demo applications. In the first demo, the control is responsible to update the progress. In the second demo, another source is responsible to update the progress.

Leave a Reply

Your email address will not be published.