B4J Question How to avoid javaobject method blocking the ui thread

xulihang

Active Member
Licensed User
Longtime User
I am using jgit with the code below to push git commits to GitHub. The push method will take time and the program will be non-responding for a while.

How to avoid ui thread being blocked?

B4X:
Sub Class_Globals
    Private fx As JFX
    Private gitJO As JavaObject
    Private gitJOStatic As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(path As String)
    gitJOStatic.InitializeStatic("org.eclipse.jgit.api.Git")
    If File.Exists(path,".git")=False Then
        Log("init")
        init(path)
    End If
    gitJO=gitJOStatic.RunMethodJO("open",Array(getFile(path)))
End Sub

Sub getFile(path As String) As JavaObject
    Dim fileJO As JavaObject
    fileJO.InitializeNewInstance("java.io.File",Array(path))
    Return fileJO
End Sub

Public Sub init(path As String)
    gitJOStatic.RunMethodJO("init",Null).RunMethodJO("setDirectory",Array(getFile(path))).RunMethodJO("call",Null)
End Sub

Public Sub push(username As String,password As String,remoteName As String,branchName As String) As ResumableSub
    Sleep(0)
    Try
        Dim PushCommand As JavaObject
        PushCommand=gitJO.RunMethodJO("push",Null)
        PushCommand.RunMethodJO("setRemote",Array(remoteName))
        PushCommand.RunMethodJO("add",Array(branchName))
        If username<>"" Then
            Dim cp As JavaObject
            cp=setCredentialProvider(username,password)
            PushCommand.RunMethod("setCredentialsProvider",Array(cp))
        End If
        PushCommand.RunMethodJo("call",Null)
    Catch
        Log(LastException)
        Return "error"&LastException.Message
    End Try
    Return "success"
End Sub

B4X:
wait for (projectGit.push(username,password,"origin","master")) complete (result As String)
    If result.StartsWith("error") Then
        fx.Msgbox(Main.MainForm,"Push Failed","")
    End If
 

Roycefer

Well-Known Member
Licensed User
Longtime User
If there are no asynchronous versions of the methods you call inside projectGit.push(), then you will probably have to use the Threading library to make your own asynchronous method.
 
Upvote 0
Top