[Gambas-user] Shell Wait without Freeze Program
Tobias Boege
tobs at taboege.de
Sun May 16 10:25:44 CEST 2021
On Sat, 15 May 2021, Hasan Merkit wrote:
> How i can make waiting Shell without freezes program?
>
> When i use this:
> Shell “bash /etc/teteosnet/temp/piluxupdate_beta1.sh” Wait
>
> Application executing this SH file, it’s fine but while executing, after few seconds OS warn user “My Gambas3 App not responsing. [Wait] [Force Quit]”
>
> Just i want make my program wait to end of script, but not freeze.
>
Gambas is single-threaded and uses an event loop to handle the multiple
sources of events in your program.
When you use "Shell ... Wait", you tell Gambas to suspend the event loop
and just wait for the command to finish. During this time, no mouse or
keyboard events are processed and your desktop judges that the program
is not responding. The thought here is that you apparently don't want
other code in your program to run while your script is not finished and
therefore the event loop, the main source of code being run asynchronously,
must be disabled -- and you're asking for that to be done with "Wait".
If you want to process keyboard and mouse events while the script process
runs in the background, you want the event loop to be active. This means
NOT using the "Wait" option. It also means that all sorts of event-driven
code in your project can potentially be invoked while your script runs.
You should be aware that these are the only two possibilities: either
block all events and appear frozen (in this case, your script runs safely
to completion before Gambas carries on) or continue to process events
(concurrently with the running of the script) and take the necessary
precautions in your code to handle the concurrency.
> Example:
>
> Private Executing As Boolean = False
> Public Sub Form_Open()
> Executing = True
> Shell “meow.sh” Wait
> Executing = False
> End
>
> Public Sub Form_Close()
> Stop Event
> If Executing Then
> Message(“Please wait”)
> Else
> Me.Close
> Endif
> End
With the explanation above, you can now understand that the Executing
variable in this example is useless. There is no part of your Gambas
code which will ever see Executing as True. The Form_Close event cannot
be triggered during "Shell ... Wait".
Finally, here is a version which does not freeze, by not using "Wait",
and therefore every Gambas event might also happen while the meow.sh
script is running:
Private $hMeow As Process
Public Sub Form_Open()
$hMeow = Shell "meow.sh" ' better: Exec ["meow.sh"]
End
Public Sub Form_Close()
If $hMeow And $hMeow.State = Process.Running Then
Stop Event
Message("Please wait")
Endif
End
If you need to react to meow.sh finishing or crashing or sending data,
use the events of the Process class [1]. Here is a slightly longer
example which uses Application.Busy to indicate that meow.sh is running
and your program is busy waiting for it. The example also echos meow's
standard output to the console:
Private $hMeow As Process
Public Sub Form_Open()
$hMeow = Exec ["meow.sh"] As "Meow"
Inc Application.Busy
End
Public Sub Meow_Kill()
Dec Application.Busy
End
Public Sub Meow_Read()
Print "meow.sh says:";; Last.ReadLine()
End
Public Sub Form_Close()
If $hMeow And $hMeow.State = Process.Running Then
Stop Event
Message("Please wait")
Endif
End
Please experiment with this and do ask back if it doesn't work.
I wrote it off the top of my head without testing. It may be that
the Form_Close logic is redundant as long as Application.Busy is
positive.
Best,
Tobias
[1] http://gambaswiki.org/wiki/comp/gb/process
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User
mailing list