472,958 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

CountDown Timer

I've got a Background Worker Thread that can take a long time.

I've got a Progress Bar that updates, but I'd also like to include something
about the time remaining that I can place on the status bar.

I created a DateTime object that is visible to class, and I store
DateTime.Now whenever the Background Worker's ProgressChanged occurs. To get
the amount of time that has lapsed, I subtract the Ticks of this object with
the current DateTime.

But, how do I get (long)Ticks converted back to a DateTime?

How would I display the DateTime to show the time remaining? The
calculations are easy enough - I just don't know what is the best method of
displaying the time left.

Or...

Is there something built in that I could use? Or is there an easier way?

Looking for solutions, ideas, links, or thoughts! :)
Aug 1 '08 #1
4 3591
On Aug 1, 10:41*am, jp2msft <jp2m...@discussions.microsoft.comwrote:
I've got a Background Worker Thread that can take a long time.

I've got a Progress Bar that updates, but I'd also like to include something
about the time remaining that I can place on the status bar.
That depend if you know how long the background process can take, or
at least how long each steps takes, if you can do that then all you
need to do is send an event to the UI thread using Control.Invoke
Aug 1 '08 #2
The thread is processing records from a database. After the records are
pulled, I can set the Progress Bar's Maximum Value to the record count and
set the Progress Bar's Value to the current record number.

The process takes 1 to 3 seconds or up to 10 minutes. It just depends how
many records there are and which of the calculations have been selected.

I can calculate the timespan between entries into the Progress Changed
event, but I don't seem to be calculating the time correctly.

In the example code below, my denominator is always 0. Any ideas?

private void TreeThread_ProgressChanged(object sender,
ProgressChangedEventArgs e) {
if (m_parent.ProgressBar1.Visible == false) {
m_parent.ProgressBar1.Value = 0;
m_parent.ProgressBar1.Visible = true;
m_threadTime = DateTime.Now;
}
if (e.ProgressPercentage != 0) {
if (m_parent.ProgressBar1.Maximum != m_objTree.Total) {
m_parent.ProgressBar1.Maximum = m_objTree.Total;
}
try {
TimeSpan span = (DateTime.Now - m_threadTime);
m_objTree.Count = e.ProgressPercentage;
string statusMsg = string.Format("Building Report (Row {0} of
{1})", m_objTree.Count, m_objTree.Total);
if (0 < span.Ticks) {
long denominator = (long)(m_objTree.Count -
m_parent.ProgressBar1.Value) / span.Ticks; // Interval
if (0 < denominator) {
long numerator = (long)(m_objTree.Total - m_objTree.Count); //
amount left
long estimate = numerator / denominator;
statusMsg += string.Format(" ...estimate {0}:{1} time
remaining)", (estimate / 60).ToString("00"), (estimate % 60).ToString("00"));
}
}
m_parent.ProgressBar1.Value = m_objTree.Count;
UpdateStatusBar(statusMsg);
m_threadTime = DateTime.Now;
} catch (Exception error) {
Console.WriteLine("Progress Error: " + error.Message);
}
}
}


"Ignacio Machin ( .NET/ C# MVP )" wrote:
On Aug 1, 10:41 am, jp2msft <jp2m...@discussions.microsoft.comwrote:
I've got a Background Worker Thread that can take a long time.

I've got a Progress Bar that updates, but I'd also like to include something
about the time remaining that I can place on the status bar.

That depend if you know how long the background process can take, or
at least how long each steps takes, if you can do that then all you
need to do is send an event to the UI thread using Control.Invoke
Aug 1 '08 #3
On Aug 1, 12:31*pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
The thread is processing records from a database. After the records are
pulled, I can set the Progress Bar's Maximum Value to the record count and
set the Progress Bar's Value to the current record number.
That's fine, just remember to always use Invoke
The process takes 1 to 3 seconds or up to 10 minutes. It just depends how
many records there are and which of the calculations have been selected.
then you will update the bar based on the record count and not the
time, you could say for example that an event is fired every 5 or 10
records. then it will update the progress bar,
one suggestion, you should also use a Label to indicate the progress,
a la "processing record X of XXXX"
Aug 1 '08 #4
Eureka! I got it.

For those that want to see the solution, here it is:

private void TreeThread_ProgressChanged(object sender,
ProgressChangedEventArgs e) {
m_objTree = (TreeParams)e.UserState;
if (m_parent.ProgressBar1.Visible == false) {
m_parent.ProgressBar1.Value = 0;
m_parent.ProgressBar1.Visible = true;
m_threadTime = DateTime.Now;
}
if (e.ProgressPercentage != 0) {
if (m_parent.ProgressBar1.Maximum != m_objTree.Total)
m_parent.ProgressBar1.Maximum = m_objTree.Total;
try {
TimeSpan span = (DateTime.Now - m_threadTime);
string statusMsg = string.Format("Building Tree (Row {0} of {1})",
m_objTree.Count, m_objTree.Total);
if (0 < span.Ticks) {
decimal denominator = (decimal)(m_objTree.Count -
m_parent.ProgressBar1.Value) / span.Ticks; // Interval
if (0 < denominator) {
long numerator = (long)(m_objTree.Total - m_objTree.Count); //
amount left
span = new TimeSpan((long)((decimal)numerator / denominator));
statusMsg += string.Format(" ...estimate {0}:{1} time remaining)",
span.Minutes.ToString("00"), span.Seconds.ToString("00"));
}
}
m_parent.ProgressBar1.Value = m_objTree.Count;
UpdateStatusBar(statusMsg);
m_threadTime = DateTime.Now;
} catch (Exception error) {
Console.WriteLine("TreeThread Progress Error: " + error.Message);
}
}
}

Obviously, my variables are irrelivant; but, the average developer should be
able to taylor the code to its needs.
Aug 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bonnett | last post by:
I have been creating a generic countdown timer (source code below), counting the seconds, minutes, hours and days till an event, but I have having trouble with it finding out how many hours are...
4
by: Christine | last post by:
I've implemented a countdown timer for a tutorial web page that should give the user 45 minutes to complete the test, only to find that the timer is slowly 'losing' time. On average, it actually...
8
by: Michael | last post by:
I have this script that works the way I want except for one thing... Once it hits zero it starts to count up and looks like this: -1:0-1:0-1:0-18 with the last number counting up. Can anyone...
1
by: Will | last post by:
I have form showing instructions and logging actions and I would like the form to show a count down timer for a certain period of time. E.g. 30 secs, going down to 0 and then displaying the action....
3
by: JimJam | last post by:
Hello Peeps I have created a program in VB.NET 2003 that counts down from 7.5 hours to zero from 08:00. I have a button that starts a new countdown from 1 hour to zero whilst the first one is...
1
by: Dirk Hagemann | last post by:
Hi! I'd like to implement a countdown timer on a webite. It should show the months, days, hours, minutes and seconds until a given date and time. So far it's not really difficult, but this...
9
by: Yet Another One | last post by:
Sorry if this has been answered before, but I simply cannot find the solution on my own. I am looking to do a countdown in my application. Basically, I need to count down from 5:59 (5 minutes,...
9
by: MC | last post by:
I would like to display a timer in the corner of the page that shows the user how many minutes:seconds till the session times out. Are there any good examples out there? Google has again failed...
5
mageswar005
by: mageswar005 | last post by:
hi, I need a javascript countdown timer to my website, I have already the countdown timer but its have some problem. Note: 1) If i choose the tools/internet option in my browser means , at...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.