B4J Question HttpUtils2 job Finished

Discussion in 'B4J Questions' started by klingon467, Sep 10, 2015.

  1. klingon467

    klingon467 Member Licensed 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

    Code:
    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!
     
  2. Roycefer

    Roycefer Well-Known Member Licensed 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.
     
    klingon467 likes this.
  3. klingon467

    klingon467 Member Licensed User

    You can give an example? o_O
    thanks
     
  4. Roycefer

    Roycefer Well-Known Member Licensed User

    Code:
    Sub Process_Globals
         
    Dim jobs(3as HttpJob
         
    Dim jobisfinished(3as Boolean = Array As Boolean(FalseFalseFalse)
    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(0And jobisfinished(1And jobisfinished(2Then
              FinishDownload
         
    End If
    End Sub
     
    klingon467 and inakigarm like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice