Show remaining time with a progress bar

amadman114amadman114 Member Posts: 36
edited 2013-11-12 in NAV Tips & Tricks
I have further updated this code so that the time remaining is properly shown in a Minute:Seconds format

I took the time to put it into a codeunit, so hopefully it will be moved into the tips and tricks section where others can benefit from it.

I attached my codeunit, and I also attached my example usage of it. Let me know if you need it in a different format (I'm still new to this!)

-Dan

Normal Codeunit:
OBJECT Codeunit 123456785 RemainingTime
{
  OBJECT-PROPERTIES
  {
    Date=08/11/13;
    Time=17:17:05;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
          END;

  }
  CODE
  {
    VAR
      ElapsedTime@1000000000 : Integer;
      RemainingMins@1000000002 : Decimal;
      RemainingSeconds@1000000003 : Decimal;
      RoundedRemainingMins@1000000004 : Integer;

    PROCEDURE RemainingTime@1000000000(ProgressI@1000000001 : Integer;ProgressTotal@1000000002 : Integer;StartTime@1000000000 : Time) TimeLeft : Text[30];
    BEGIN
      // Created by Dan Steele
      // Mibuso user amadman114

      ElapsedTime := ROUND(((TIME - StartTime) / 1000),1);
      RemainingMins := ROUND((((ElapsedTime / ((ProgressI/ProgressTotal) * 100) * 100)-ElapsedTime)/60),0.1);
      RoundedRemainingMins := ROUND(RemainingMins,1,'<');
      RemainingSeconds := ROUND(((RemainingMins - RoundedRemainingMins)*0.6)*100,01);
      TimeLeft := FORMAT(RoundedRemainingMins)+':'+FORMAT(RemainingSeconds);


      //Where ProgressI is the increasing value of the number of records completed ProgressTotal
      // is the total number of records and StartTime is the time when the process started.
    END;

    BEGIN
    END.
  }
}


Codeunit With example "Parent" codeunit:
OBJECT Codeunit 123456785 RemainingTime
{
  OBJECT-PROPERTIES
  {
    Date=08/11/13;
    Time=17:17:05;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
          END;

  }
  CODE
  {
    VAR
      ElapsedTime@1000000000 : Integer;
      RemainingMins@1000000002 : Decimal;
      RemainingSeconds@1000000003 : Decimal;
      RoundedRemainingMins@1000000004 : Integer;

    PROCEDURE RemainingTime@1000000000(ProgressI@1000000001 : Integer;ProgressTotal@1000000002 : Integer;StartTime@1000000000 : Time) TimeLeft : Text[30];
    BEGIN
      // Created by Dan Steele
      // Mibuso user amadman114

      ElapsedTime := ROUND(((TIME - StartTime) / 1000),1);
      RemainingMins := ROUND((((ElapsedTime / ((ProgressI/ProgressTotal) * 100) * 100)-ElapsedTime)/60),0.1);
      RoundedRemainingMins := ROUND(RemainingMins,1,'<');
      RemainingSeconds := ROUND(((RemainingMins - RoundedRemainingMins)*0.6)*100,01);
      TimeLeft := FORMAT(RoundedRemainingMins)+':'+FORMAT(RemainingSeconds);


      //Where ProgressI is the increasing value of the number of records completed ProgressTotal
      // is the total number of records and StartTime is the time when the process started.
    END;

    BEGIN
    END.
  }
}

OBJECT Codeunit 123456788 CalcInvAmount
{
  OBJECT-PROPERTIES
  {
    Date=08/11/13;
    Time=17:18:58;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
          END;

  }
  CODE
  {
    VAR
      GrecSalesInvLine@1000000000 : Record 113;
      GLastYear@1000000001 : Date;
      GrecSalesInvHeader@1000000002 : Record 112;
      Total@1000000003 : Decimal;
      InvAmount@1000000004 : Decimal;
      intProgressI@1000000005 : Integer;
      intProgressTotal@1000000006 : Integer;
      intProgress@1000000007 : Integer;
      diaProgress@1000000008 : Dialog;
      timProgress@1000000009 : Time;
      UpdateForm@1000000010 : Form 123456784;
      ElapsedTime@1000000011 : Integer;
      RemainingTime@1000000012 : Text&#91;30&#93;;
      StartTime@1000000013 : Time;
      RemainingMins@1000000014 : Decimal;
      RemainingMinsAndone@1000000015 : Decimal;
      RoundedRemainingMins@1000000016 : Integer;
      GRecRange@1000000017 : Date;
      cduRemainingTime@1000000018 : Codeunit 123456785;
      TimeLeft@1000000019 : Text&#91;30&#93;;

    PROCEDURE Update@1000000005(UpdateType@1000000000 : Option);
    BEGIN

      CASE UpdateType OF
        0 : GRecRange := CALCDATE('-1D',TODAY);
        1 : GRecRange := CALCDATE('-1W',TODAY);
        2 : GRecRange := CALCDATE('-1M',TODAY);
        3 : GRecRange := CALCDATE('-1Y',TODAY);
        4 : GRecRange := CALCDATE('-2Y',TODAY);
      END;


      intProgressI := 0;diaProgress.OPEN('#1####\#2####\@3@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',ElapsedTime,TimeLeft,intProgress);


      GrecSalesInvLine.RESET;
      GrecSalesInvHeader.RESET;


      GrecSalesInvHeader.SETRANGE("Posting Date",GRecRange, TODAY);          // Only Updates Records in the last year
      intProgressTotal := GrecSalesInvHeader.COUNT;
      GrecSalesInvHeader.FINDFIRST;

      timProgress := TIME;
      StartTime := TIME;

      REPEAT


        CLEAR(InvAmount);
        GrecSalesInvLine.SETRANGE("Document No.",GrecSalesInvHeader."No.");       // Creates a loop too look for all instances of the same
                                                                                  // Document No.
          REPEAT

            InvAmount := InvAmount + GrecSalesInvLine.Amount;

          UNTIL GrecSalesInvLine.NEXT = 0;

        GrecSalesInvHeader.Amount := InvAmount;
        GrecSalesInvHeader.MODIFY;



                     //Progress bar -----------------
                          intProgressI := intProgressI + 1;
                          IF timProgress &lt; TIME - 1000 THEN BEGIN // every second
                           timProgress := TIME;
                           intProgress := ROUND(intProgressI / intProgressTotal * 10000,1);
                           diaProgress.UPDATE;
                           END;

      TimeLeft := cduRemainingTime.RemainingTime(intProgressI,intProgressTotal,StartTime);
      ElapsedTime := ROUND(((TIME - StartTime) / 1000),1);


                    //Progress bar ----------------
      UNTIL GrecSalesInvHeader.NEXT = 0;
      diaProgress.CLOSE;





    END;

    BEGIN
    {
      DJS Nov 2013
    }
    END.
  }
}

Sign In or Register to comment.