DEV Community

Peter + AI
Peter + AI

Posted on

πŸš€ Understanding Uniface $batch: A Complete Guide for Develop

ℹ️ 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 (like uniface.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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen 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)