New-UDStepper - OnFinish multiple execution

Product: PowerShell Universal
Version: 5.5.1

Hi,

does anyone has an idea or knows how to prevent steppers to execute the -OnFinish scriptblock twice or more times when clicking fast on “finish”?

Code to test:

new-udstepper -id "main_stepper" -children {
    new-udstep -label "just a step 1" -onload {
        new-udtextbox -label "jsut a empty box, for now" -id "textbox_mytextbox"
    }
    new-udstep -label "just a summary" -onload {
        $bodydata = $body | convertfrom-json
        new-udtypography -text "test my input: " -fontweight "bold"
        new-udtypography -text $bodydata.context.textbox_mytextbox
    }
} -onfinish {
    $bodydata = $body | convertfrom-json
    show-udtoast -message "$(get-date -format u) - $($bodydata.textbox_mytextbox)" -duration 5000
}

I’m not able to replicate your issue. No matter how fast I click on finish. So your issue might be with your setup.

But anyways this is some pseudo code that might help - If you use a combination of If and try-catch-finally, you can control how many times your code is executed on the onfinish part. Then you can either add null to your body and use that with an IF statement or just remove the last element from the stepper.

I’ve done both below, but I can’t test it on my setup, so you will have to play around with it.


New-UDApp -Content {

    new-udstepper -id "main_stepper" -children {
        new-udstep -label "just a step 1" -onload {
            new-udtextbox -label "jsut a empty box, for now" -id "textbox_mytextbox"
        }
        new-udstep -label "just a summary" -onload {
            $bodydata = $body | convertfrom-json
            new-udtypography -text "test my input: " -fontweight "bold"
            new-udtypography -text $bodydata.context.textbox_mytextbox
        } -Id "FinalStep"
    } -onfinish {

        If ($null -eq $body) {

            # Do nothing

        } else {


            try {

            $bodydata = $body | convertfrom-json
            show-udtoast -message "$(get-date -format u) - $($bodydata.textbox_mytextbox)" -duration 5000
           
            
            }
            catch {
                <#Do this if a terminating exception happens#>
            }
            finally {
                Remove-UDElement -Id "FinalStep"
                $body = $null
            }

            

        }
    }
}

Hi Coconuts, thanks for your reply.
If you add some more processing in the OnFinish scriptblock you can replicate the issue for example:

 -onfinish {
    $bodydata = $body | convertfrom-json
    1..10000 | foreach-Object { write-host "test $_" }
    show-udtoast -message "$(get-date -format u) - $($bodydata.textbox_mytextbox)" -duration 5000
}

And for our usecase, we execute pretty big functions in the -OnFinish scriptblock which starts some service requests and the problem is, some my co-workers are pretty impatient if the last step persists so they click twice or more on the on finish button.

I’ve already tried to remove the last step with Remove-UDElement, this would solve the problem but it creates another one:

Nevermind,I found a solution using $page: variables.
If anyone runs into this problem:

$Page:OnFinishLimit = 0
new-udstepper -id "main_stepper" -children {
    new-udstep -label "just a step 1" -onload {
        new-udtextbox -label "jsut a empty box, for now" -id "textbox_mytextbox"
    }
    new-udstep -label "just a summary" -onload {
        $bodydata = $body | convertfrom-json
        new-udtypography -text "test my input: " -fontweight "bold"
        new-udtypography -text $bodydata.context.textbox_mytextbox
    } -id "step_finalStep"
} -onfinish {
    if(++$Page:OnFinishLimit -gt 1) {
        continue
    } else {
        $bodydata = $body | convertfrom-json
        1..10000 | foreach-Object { write-host "test $_" }
        show-udtoast -message "$(get-date -format u) - $($bodydata.textbox_mytextbox)" -duration 5000
    }
}
1 Like