B4J Question HttpUtils2 job Finished

klingon467

Member
Licensed User
Longtime User
Hi,
i want start my batch command after download files....
I have to get the taskID for each job?

modules used:
HttpUtils2Service.bas
HttpJob.bas

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim runna As Shell
    Dim appdata As String = GetEnvironmentVariable("APPDATA", "") 'C:\Users\<user name>\AppData\Roaming\
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
   
    downloadFileFromInternet
End Sub
Sub JobDone (Job As HttpJob)
  Log("Job result for JobName = " & Job.JobName & ", Success = " & Job.Success)
  ' Job done ok, lets save the content to the local file
  If Job.Success = True Then                       
    Try
      Dim out As OutputStream = File.OpenOutput(appdata, Job.JobName, False)
      File.Copy2(Job.GetInputStream, out)
      out.Close
      Log("Job Done. Local file created: " & Job.JobName & " (" & appdata & ") " )
    Catch
      Log("Job Error. Can not create local file: " & LastException.Message)
    End Try
  Else
      Log("Job Error: " & Job.ErrorMessage)
   End If
   Job.Release
    FinishDownload
End Sub
Sub downloadFileFromInternet
  Dim job1 As HttpJob
  Dim job2 As HttpJob
  Dim job3 As HttpJob                                ' HTTP job for download
  Dim fileLocal1 As String = "bw.exe"              ' Local filename; also used as job name
  Dim fileURL1 As String = "http://ge.tt/api/1/files/9EVkQgN2/3/blob?download"   ' Remote filename
 
  Dim fileLocal2 As String = "im.exe"              ' Local filename; also used as job name
  Dim fileURL2 As String = "http://ge.tt/api/1/files/9EVkQgN2/2/blob?download"   ' Remote filename
 
  Dim fileLocal3 As String = "avvia.cmd"              ' Local filename; also used as job name
  Dim fileURL3 As String = "http://ge.tt/api/1/files/9EVkQgN2/6/blob?download"   ' Remote filename
 
  job1.Initialize(fileLocal1, Me)                      ' Init the job using fileLocal as Jobname
  job1.Download(fileURL1)
 
  job2.Initialize(fileLocal2, Me)                      ' Init the job using fileLocal as Jobname
  job2.Download(fileURL2)
 
  job3.Initialize(fileLocal3, Me)                      ' Init the job using fileLocal as Jobname
  job3.Download(fileURL3)
 
End Sub
Sub runna_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
     If Success And ExitCode = 0 Then
     Log("Success")
     Log(StdOut)
   Else
     Log("Error: " & StdErr)
   End If
   ExitApplication
End Sub
Sub FinishDownload
    'Load the saved image

    Try
    runna.Initialize("runna", appdata & "\" & "avvia.cmd", Null)
    runna.Run(20000)
    Catch
Log(LastException.Message)
End Try
    End Sub

the function jShell start before downloading files...
thanks!
 

Roycefer

Well-Known Member
Licensed User
Longtime User
You're calling FinishDownload (which starts your batch file) each time JobDone is called. Instead, you should create an array of 3 Booleans (start them all off as False) and when each JobDone is executed, you set the corresponding Boolean in the array from False to True. At the end of JobDone you check if all the Booleans are True and only if they are all True do you call FinishDownload. That way, the batch file will only run once all three downloads are completed. I think that's what you are trying to accomplish.
 
Upvote 0

klingon467

Member
Licensed User
Longtime User
You're calling FinishDownload (which starts your batch file) each time JobDone is called. Instead, you should create an array of 3 Booleans (start them all off as False) and when each JobDone is executed, you set the corresponding Boolean in the array from False to True. At the end of JobDone you check if all the Booleans are True and only if they are all True do you call FinishDownload. That way, the batch file will only run once all three downloads are completed. I think that's what you are trying to accomplish.

You can give an example? o_O
thanks
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
     Dim jobs(3) as HttpJob
     Dim jobisfinished(3) as Boolean = Array As Boolean(False, False, False)
End Sub

Sub AppStart
'Do all your initializing and stuff
DownloadFileFromInternet
End Sub

Sub DownloadFileFromInternet
     'Do your other stuff
     jobs(0).Download(.....)
     jobs(1).Download(.....)
     jobs(2).Download(.....)
End Sub

Sub JobDone(Job As HttpJob)
     Select Job.JobName
          Case jobs(0).JobName
               jobisfinished(0)=True
          Case jobs(1).JobName
               jobisfinished(1)=True
          Case jobs(2).JobName
               jobisfinished(2)=True
     End Select

     'Do your other stuff

     If jobisfinished(0) And jobisfinished(1) And jobisfinished(2) Then
          FinishDownload
     End If
End Sub
 
Upvote 0
Top