Forum Discussion

shahid24's avatar
shahid24
Contributor
25 days ago
Solved

How to perform certain actions for a specific amount of time in a loop

I want to perform certain action inside a Do Loop statement, in this case i want to ensure that i get out of the loop after ten minutes have elapsed. What should be the MaxDelay+X value ? 

Sub Test

  MaxDelay = 0

Do 

Call aqUtils.Delay(1,"Reports are being signed..")

  MaxDelay=MaxDelay+ X
  
  Loop Until MaxDelay > 600000
  
End Sub

Thanks 

  • I have this function that seems to do what you want, It is written in Javascript but rewriting to VB should not be a problem.

    function DoStuff(delayMS)
    {
    let start = Date.now();
      
      while(Date.now() - start < delayMS)
      {
        if (Aliases.Stuff.Exists)
        {
          Delay(500);
          Aliases.Stuff.Close(1000);
          return;
        }
        Delay(100); //Short break before checking again
      }
    
    Log.Warning("Stuff did not exist!");
    }

    The function checks if something exists and closes it, It waits for a max of {delayMS} miliseconds 

    If it doesn't exists within the given time it posts a warning to the logs

  • Doing something have x amount of time that can not be calculated. This JavaScript code would be more realistic to expectations.

    function TimeIt()
    {
      Now = new Date().getTime();
      ExitAt = Now + 600000; // add 10 minutes in milleseconds
      while (Now < ExitAt) {
        // Do something
        Now = new Date().getTime();
      }
    }

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you search the forum, there will be examples. mainly in JavaScript.

  • MW_Didata's avatar
    MW_Didata
    Regular Contributor

    I have this function that seems to do what you want, It is written in Javascript but rewriting to VB should not be a problem.

    function DoStuff(delayMS)
    {
    let start = Date.now();
      
      while(Date.now() - start < delayMS)
      {
        if (Aliases.Stuff.Exists)
        {
          Delay(500);
          Aliases.Stuff.Close(1000);
          return;
        }
        Delay(100); //Short break before checking again
      }
    
    Log.Warning("Stuff did not exist!");
    }

    The function checks if something exists and closes it, It waits for a max of {delayMS} miliseconds 

    If it doesn't exists within the given time it posts a warning to the logs

  • Hassan_Ballan's avatar
    Hassan_Ballan
    Regular Contributor

    Doing something have x amount of time that can not be calculated. This JavaScript code would be more realistic to expectations.

    function TimeIt()
    {
      Now = new Date().getTime();
      ExitAt = Now + 600000; // add 10 minutes in milleseconds
      while (Now < ExitAt) {
        // Do something
        Now = new Date().getTime();
      }
    }