Click here to Skip to main content
12,502,337 members (51,045 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26.1K views
1.6K downloads
33 bookmarked
Posted

Display Time Progress with TimeProgressBar

, 4 Nov 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
How to display the progress of a task when its running time is known.

Introduction

The TimeProgressBar allows you to display the progress of a task when its running time is known.

While ProgressBar control can be used for this task, I wanted a control that will display more information about the progress of the task - the time passed since the start of the task, the time left to the end of the task and the percent of passed time from the total time of the task.

Using the Control

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 in the its left side. The size of this rectangle will be determined by the percent of passed time from the total time of the task(i.e.: if 60% of 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, the time passed since the start of the task
  • In the center, the percent of passed time from the total time of the task
  • In the right, the time left to the end of the task

The TimeInside property determines where those properties will be displayed. If true, the times will be displayed inside the progress bar. Otherwise, times will be displayed outside the progress bar.

Starting

To start monitoring the progress of a new task, use one of the Start methods.

  • The Start(TimeSpan duration) 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) will display progress of a task that starts in startTime and ends in endTime.

Updating

To display the progress of the task and update the control properties (TimeLeft,TimeLeftPercent, TimePass,TimePassPercent), the Update method should be called. The control will call it each second if AutoUpdate is true. Otherwise, the user is responsible for updating the contol.

Events

The following events will be raised by the control:

  • 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.

Please note that those events will call the interested parties in different threads from the gui thread so if you want to update the gui controls, you should use the `Invoke` method.

The Demo Programs

The AutoUpdate Demo Program

This demo program shows how to use the control with the default values (AutoUpdate is true). When the form is loaded, we will connect the event handlers and set the task time to 20 seconds:

//-------------------------------------------------------------------------
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.

//-------------------------------------------------------------------------
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}%\r\n",prefix,winTimeProgresBar.TimePassPercent);
  winText.AppendText(ss);
}
//------------------------------------------------------------------------- 

The NoAutoUpdate Demo Program

While setting AutoUpdate to true is convenient, sometimes you may want that the other 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 that will be responsible for updating the control.

//-------------------------------------------------------------------------
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.

//-------------------------------------------------------------------------
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}%\r\n",prefix,winTimeProgresBar.TimePassPercent);
  winText.AppendText(ss);
}
//-------------------------------------------------------------------------   

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

napuzba
Israel Israel
I am kobi, a passionate software developer.
Actively seeking for new opportunities. Please feel free to contact me via my blog.
  • I'm working in C#, C++, PHP and Python
  • I'm building websites, mobile applications and desktop software.

You may also be interested in...

Comments and Discussions

 
QuestionWhat Session? Pin
Eddy Vluggen19-Jun-12 2:39
memberEddy Vluggen19-Jun-12 2:39 
AnswerRe: What Session? Pin
zebrot19-Jun-12 19:14
memberzebrot19-Jun-12 19:14 
GeneralRe: What Session? Pin
Eddy Vluggen20-Jun-12 1:16
memberEddy Vluggen20-Jun-12 1:16 
GeneralRe: What Session? Pin
BillW3328-Oct-15 3:22
professionalBillW3328-Oct-15 3:22 
QuestionNice! Pin
Ravi Bhavnani19-Jun-12 2:18
memberRavi Bhavnani19-Jun-12 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 4 Nov 2015
Article Copyright 2015 by napuzba
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid