Write responsive asynchronous workflows
Keep the UI usable while work runs, and restore a truthful state on every exit path.
Use Await where the compiler supports it#
Mark a procedure Async before using Await. McBasic™ currently accepts Await as a direct scalar assignment or an explicit Await statement, with a parenthesized asynchronous call. Split compound expressions into local steps; this also gives failures a clear place to be handled.
Dim envelopeText As String
envelopeText = Await Http.RequestAsync("GET", url, "{}", "", 10000)
response = Json.Parse(envelopeText)Guard against overlapping actions#
A disabled button helps the user, but an explicit busy guard protects the procedure too. Set the flag before starting work. Disable other actions that depend on the same state and show a short status explaining what is happening. Do not clear existing records merely because a refresh has begun.
Handle three outcomes#
| Outcome | What to show | What to preserve |
|---|---|---|
| Success | The actual result after the operation completes. | The new committed state. |
| Cancellation | That the user stopped the operation. | The last valid state, according to the API’s cancellation contract. |
| Failure | A useful message and a safe retry path. | Previous valid data; do not pretend failure means an empty successful result. |
Catch McException to inspect Number and Message. Finally is the place to restore commands and stop progress timers. It runs even when an earlier branch exits the procedure.
Own what you create#
Use Using for disposable objects where the API supports it. A cancellation source and a download operation have lifetimes; they should not become abandoned background work after their Form closes. The complete download lab combines Using, progress, cancellation and delayed close.
Test timing, not just data#
- Activate the action twice quickly.
- Cancel before completion.
- Close the Form while busy.
- Try a timeout or unreachable endpoint.
- Retry after failure and verify commands were restored.
- Check that a slow earlier answer cannot overwrite a newer user decision.
20f4bfed6b · Build manifest