CountDown Timer

J

jp2msft

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! :)
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 
J

jp2msft

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);
}
}
}
 
I

Ignacio Machin ( .NET/ C# MVP )

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"
 
J

jp2msft

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top