CountDown Timer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?anAybXNmdA==?=

    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! :)
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: CountDown Timer

    On Aug 1, 10:41 am, jp2msft <jp2m...@discus sions.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

    Comment

    • =?Utf-8?B?anAybXNmdA==?=

      #3
      Re: CountDown Timer

      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_Prog ressChanged(obj ect sender,
      ProgressChanged EventArgs e) {
      if (m_parent.Progr essBar1.Visible == false) {
      m_parent.Progre ssBar1.Value = 0;
      m_parent.Progre ssBar1.Visible = true;
      m_threadTime = DateTime.Now;
      }
      if (e.ProgressPerc entage != 0) {
      if (m_parent.Progr essBar1.Maximum != m_objTree.Total ) {
      m_parent.Progre ssBar1.Maximum = m_objTree.Total ;
      }
      try {
      TimeSpan span = (DateTime.Now - m_threadTime);
      m_objTree.Count = e.ProgressPerce ntage;
      string statusMsg = string.Format(" Building Report (Row {0} of
      {1})", m_objTree.Count , m_objTree.Total );
      if (0 < span.Ticks) {
      long denominator = (long)(m_objTre e.Count -
      m_parent.Progre ssBar1.Value) / span.Ticks; // Interval
      if (0 < denominator) {
      long numerator = (long)(m_objTre e.Total - m_objTree.Count ); //
      amount left
      long estimate = numerator / denominator;
      statusMsg += string.Format(" ...estimate {0}:{1} time
      remaining)", (estimate / 60).ToString("0 0"), (estimate % 60).ToString("0 0"));
      }
      }
      m_parent.Progre ssBar1.Value = m_objTree.Count ;
      UpdateStatusBar (statusMsg);
      m_threadTime = DateTime.Now;
      } catch (Exception error) {
      Console.WriteLi ne("Progress Error: " + error.Message);
      }
      }
      }




      "Ignacio Machin ( .NET/ C# MVP )" wrote:
      On Aug 1, 10:41 am, jp2msft <jp2m...@discus sions.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
      >

      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: CountDown Timer

        On Aug 1, 12:31 pm, jp2msft <jp2m...@discus sions.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"

        Comment

        • =?Utf-8?B?anAybXNmdA==?=

          #5
          RE: CountDown Timer

          Eureka! I got it.

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

          private void TreeThread_Prog ressChanged(obj ect sender,
          ProgressChanged EventArgs e) {
          m_objTree = (TreeParams)e.U serState;
          if (m_parent.Progr essBar1.Visible == false) {
          m_parent.Progre ssBar1.Value = 0;
          m_parent.Progre ssBar1.Visible = true;
          m_threadTime = DateTime.Now;
          }
          if (e.ProgressPerc entage != 0) {
          if (m_parent.Progr essBar1.Maximum != m_objTree.Total )
          m_parent.Progre ssBar1.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_obj Tree.Count -
          m_parent.Progre ssBar1.Value) / span.Ticks; // Interval
          if (0 < denominator) {
          long numerator = (long)(m_objTre e.Total - m_objTree.Count ); //
          amount left
          span = new TimeSpan((long) ((decimal)numer ator / denominator));
          statusMsg += string.Format(" ...estimate {0}:{1} time remaining)",
          span.Minutes.To String("00"), span.Seconds.To String("00"));
          }
          }
          m_parent.Progre ssBar1.Value = m_objTree.Count ;
          UpdateStatusBar (statusMsg);
          m_threadTime = DateTime.Now;
          } catch (Exception error) {
          Console.WriteLi ne("TreeThread Progress Error: " + error.Message);
          }
          }
          }

          Obviously, my variables are irrelivant; but, the average developer should be
          able to taylor the code to its needs.

          Comment

          Working...