B4J Question Update display while code is executing

Paul.Johnson

New Member
(warning: new B4J user!)
I've just started working with B4J. I have a simple routine that creates a batch file based on some parameters and inputs, then executes the batch file. After completion, other actions are available to the user.

B4X:
        Label1.Text="please wait: executing external process..."
        lst.Initialize
        lst.add(":: first line of batch file")
        lst.add(":: second line of batch file")
        lst.add(":: and so on...")

        File.WriteList(pathStore,"c:\tempBatchFile.bat",lst)
        lst.Clear
        lst.Add("/c")
        lst.Add("""c:\tempBatchFile.bat""")
        Dim sh As Shell
        sh.Initialize("test","cmd.exe",lst)
        sh.RunSynchronous(-1)
     
        Label1.Text=tStr & " pull executed"

The process works great, but the batch file requires several seconds to execute, and there is no indication to the user that the process has started.
I have a label that I intended to use to show status (Label1), but the UI is not updated until the process has been completed. How can I display an updated label that is visible while the batch process is running? Similarly, can I change the mouse cursor during execution? With VBA I could use 'DoEvents' and/or Repaint to tell the application to update the display before proceeding, but have not discovered how to accomplish that with B4J.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Never use Shell.RunSynchronous in a UI application. It will freeze your app until the process exits.

Correct code:
B4X:
sh.Run(-1)
Wait For test_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
Log("Done")
If Success And ExitCode = 0 Then
Log("Success")
End If

Search for resumable subs to learn more about Wait For.
 
Upvote 0
Top