βΉοΈ This blog post was created with AI assistance to help developers understand Uniface better.
π€ What is $batch in Uniface?
The $batch
function in Uniface 10.4 is a simple but powerful tool that tells you whether your Uniface application is running in batch mode or interactive mode. Think of it as a switch that helps your application behave differently depending on how it's being used.
π Return Values Explained
The function returns three possible values:
- 1 π’ - Your application is running in batch mode (no user interaction)
- 0 π΅ - Your application is running interactively (user can interact)
- Negative number π΄ - An error occurred (check $procerror for details)
π§ When Does $batch Return 1?
Uniface automatically sets $batch
to 1 in these situations:
- When you start Uniface with the
/bat
command (likeuniface.exe /bat
) - When the Uniface Server (userver.exe) is executing your component
- When running remote services
- When processing dynamic or static server pages
β οΈ Important Warnings
Batch Mode Limitations: π¨
When $batch
returns 1, you cannot use:
-
askmess
- asking user for input -
edit
statements - editing on screen - Any keyboard input operations
- Any screen output operations
Why? Because there's no user sitting at the computer to respond! Trying to use these might crash your application. π₯
π‘ Practical Example
Here's a real-world example from the documentation that shows how to use $batch
properly:
if ($batch = 1)
putmess "%%$page pages sent to printer at %%$clock"
exit(0)
else
askmess "Return to Main menu or Quit? (M/Q)","M,Q"
if ($status = 1)
exit "mainmenu"
else
apexit
endif
endif
What this code does:
- π¨οΈ If in batch mode: Writes a message about printed pages and exits
- π£οΈ If interactive: Asks the user what they want to do next
π οΈ Advanced Usage
You can also change the value of $batch
:
$batch = !$batch ; Toggle batch mode
set $batch ; Set to batch mode
reset $batch ; Set to interactive mode
Boolean Logic: Since $batch
works like a Boolean (true/false), any non-zero value becomes 1, and zero stays 0.
π Web Applications
If you need to check specifically whether your service is running in a web application, use $web
instead of $batch
. Both functions serve different purposes! π
π― Best Practices
- Always check
$batch
before using interactive functions - Use conditional logic to provide different behavior for batch vs interactive
- Remember that remote services always run in batch mode
- Test your code in both modes during development
π Conclusion
The $batch
function is essential for creating flexible Uniface applications that work both interactively and in automated scenarios. By properly checking this value, you can ensure your application behaves correctly whether it's being used by a person or running automatically in the background. π
Remember: when in doubt, check $batch
before trying to interact with users! π
Top comments (0)