Android Question is it possible to use two job in a single activity

Sofiya

Member
Licensed User
i want to call two different stored procedure in one activity, is it possible? in that case is i need to use two different job done function. pls guide me thanks in advance
 

tigrot

Well-Known Member
Licensed User
Longtime User
No, you can use the job name to differenciate the two jobs. They can be many jobs.
My little example
B4X:
Sub JobDone(Job As HttpJob)
    If Job.Success=False Then
        Select Case Job.JobName
            Case "httpparam"
                If securestrin="" Then
                    ToastMessageShow(loc.Localize("Server unreachable, try again"),True)
                    ExitApplication
                    Return
                End If
                startsystem
        End Select
    Else
        Select Case Job.JobName
            Case "httpparam"
                Dim risp As String=Job.GetString
                securestrin=risp
                If File.ExternalWritable = False Then
                    CallSub2(Main,"showmsg","Unable to write parameter file")
                    Return
                End If
                File.Writestring(File.DirDefaultExternal,"param",risp)
                startsystem
        End Select
    End If
    Job.release
End Sub

this a single job "httpparam" launched by
B4X:
    Dim httpdata As HttpJob
    httpdata.Initialize("httpparam",Me)

    Dim link As String= ServerIp.Trim
    httpdata.PostString(link,"")
    httpdata.GetRequest.Timeout = 20000

You can add many cases to "httpparam".
Have a nice time!
Ciao
Mauro
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Per job create a sub. For example:
B4X:
Sub job1()
   Log("Entering job1")
   Dim http1 As HttpJob
   http1.Initialize("",Me)
   http1.Download("http://www.somefakewebsite.bbe")
   wait for (http1) JobDone(http1 As HttpJob)
   If http1.Success Then
       Log("job1 success")
   Else
       Log("job1 failure")
   End If
   Log("Exiting job1")
End Sub

Sub job2()
   Log("Entering job2")
   Dim http2 As HttpJob
   http2.Initialize("",Me)
   http2.Download("http://www.somefakewebsite.beb")
   wait for (http2) JobDone(http2 As HttpJob)
   If http2.Success Then
       Log("job2 success")
   Else
       Log("job2 failure")
   End If
   Log("Exiting job2")
End Sub

Then just call them
B4X:
Log("Starting with calling two jobs")
job1
job2
Log("Done calling two jobs")
Example abbreviated output (removed Java exception output)
Starting with calling two jobs
Entering job1
Entering job2
Done calling two jobs
ResponseError. Reason: java.net.UnknownHostException: www.somefakewebsite.bbe, Response:
job2 failure
Exiting job2
ResponseError. Reason: java.net.UnknownHostException: www.somefakewebsite.beb, Response:
job1 failure
Exiting job1

Please note that the order of finishing is not guaranteed. Calling the subs (job1, job2) does not interrupt your program flow.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Per job create a sub. For example:
Another option is doing it with one sub:
B4X:
DownloadMany (Array( "http://www.somefakewebsite1.com", "http://www.somefakewebsite2.com"))

Sub DownloadMany (links As List)
    For Each link As String In links
        Dim http As HttpJob
        http.Initialize("", Me) 
        http.Download(link)
        Wait For (http) JobDone(http  As HttpJob)
        If http.Success Then
            Log("Current link: " & link)
            'other code
        Else
            Log("Error: " & http.ErrorMessage)
            ToastMessageShow("Error: " & http.ErrorMessage, True)
        End If
        http.Release
    Next
End sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Another option is doing it with one sub:
This is a good one if you need to serialize the processing of the requests (finish one request before starting the next) and you do the same type of processing of each request's result(s). As with multiple subs, this sub does not block program flow.
 
Upvote 0

Sofiya

Member
Licensed User
Code:
Sub job1()
Log("Entering job1")
Dim http1 As HttpJob
http1.Initialize("",Me)
http1.Download(
"http://www.somefakewebsite.bbe")
wait for (http1) JobDone(http1 As HttpJob)
If http1.Success Then
Log("job1 success")
Else
Log("job1 failure")
End If
Log("Exiting job1")
End Sub

Sub job2()
Log("Entering job2")
Dim http2 As HttpJob
http2.Initialize("",Me)
http2.Download(
"http://www.somefakewebsite.beb")
wait for (http2) JobDone(http2 As HttpJob)
If http2.Success Then
Log("job2 success")
Else
Log("job2 failure")
End If
Log("Exiting job2")
End Sub

Thanks OliverA your example works fine.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Wait introduces an hidden callback, nothing more than an event handler.
Not correct. Resumable subs is a very complicated compiler feature.

It is not just an event handler. It allows your sub to resume at a specific point in the exact state it was before.

See the resumable subs video tutorial for more information.
 
Upvote 0
Top