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