471,357 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 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 3525
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bonnett | last post: by
4 posts views Thread by Christine | last post: by
8 posts views Thread by Michael | last post: by
3 posts views Thread by JimJam | last post: by
1 post views Thread by Dirk Hagemann | last post: by
9 posts views Thread by Yet Another One | last post: by
9 posts views Thread by MC | last post: by
mageswar005
5 posts views Thread by mageswar005 | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.