Process cancel

pdabasic

Active Member
Licensed User
I wrote a simple image capture from url program what capture image from th net urls what read from a csv.
I use a for looping but if the url isn't response or I want't to cancel it I don't knoe how can i solve it?!

B4X:
Sub Globals
   Dim Buffer(0) As byte
   Dim curNr
   Dim beforeNumber
   Dim afterNumber
End Sub

Sub App_Start
   pgBar.New1("frmmyCapture",20,200,200,20)
   fgFilePictureCsv.New1
   Fginputbox.New1
   frmmyCapture.Show
   fgFilePictureCsv.DialogTitel="Capture Image File"
   fgFilePictureCsv.SearchPattern="*.csv"
   If fgFilePictureCsv.ShowDialog(AppPath)=True Then
      csvFile=fgFilePictureCsv.SelectedItem(0)
      tblCap.LoadCSV(csvFile,",",False,True)
      pgBar.Maximum=tblCap.RowCount-1
      tblFail.AddCol(cString,"Fail",50,False)
      beforeNumber=Fginputbox.Input("Please give the url and the add. before number","Before Number","")
      afterNumber=Fginputbox.Input("Please give the extension and the add. after number","After Number","")
   Else
      frmmyCapture.Close
   End If
End Sub

Sub DownLoad
   For i=0 To tblCap.RowCount-1
      curNr=tblCap.Cell("Column1",i)
      url=beforeNumber&curNr&afterNumber
      DownLoadFile(AppPath & "\images\"&curNr&afterNumber,url)
      pgBar.Value=i
   Next
   If tblFail.RowCount>0 Then
      tblFail.SaveCSV("FailedImage.csv",",",False)
   End If
   Msgbox("End Of Capturing")
End Sub

Sub DownLoadFile (LocalFile,URL)
   Request.New1(URL)
   Response.New1
   ErrorLabel (errHandler)
   Response.Value = Request.GetResponse
   Reader.New1(Response.GetStream,True)
   FileOpen(c1,LocalFile,cRandom)
   Writer.New1(c1,False)
   Dim buffer(4096) As byte
   count = Reader.ReadBytes(buffer(),4096)
   Do While count > 0
      Writer.WriteBytes2(buffer(),0,count)
      count = Reader.ReadBytes(buffer(),4096)
   Loop
   FileClose(c1)
   Response.Close
   frmmyCapture.Image=LocalFile
   Return
   ErrHandler:
      Response.Close
      tblFail.AddRow(curNr)
End Sub

Sub btnProcess_Click
   Download
End Sub
 

pdabasic

Active Member
Licensed User
You should use GetAsyncResponse isntead: HTTP

Hy Erel!

So I stuck in the process

My code look like this:
B4X:
Sub DownLoadFile (URL)
   Request.New1(URL)
   Response.New1
   Request.GetAsyncResponse
End Sub

Sub Request_Response
   ErrorLabel (errHandler)
   If Request.ResponseCode = 200 Then
      Response.Value = Request.AsyncResponse
      total=Round(Response.ContentLength/1000)
      If total=0 Then
         pgBarCurent.Maximum=1
         pgBarCurent.Value=1
         tblFail.AddRow(curNr)
         lblPgCurent.Text="false"
         sub_my_stream
      Else
         pgBarCurent.Maximum=total
         File=dirImage&"\"&curNr&afterNumber
         Response.GetAsyncStream(File)
         tmrCurent.Enabled = True
      End If
   Else
      tblFail.AddRow(curNr)
      lblPgCurent.Text="false"
      sub_my_stream
   End If
   Return

   ErrHandler:
      pgBarCurent.Maximum=1
      pgBarCurent.Value=1
      tblFail.AddRow(curNr)
      lblPgCurent.Text="false"
      sub_my_stream
End Sub

Sub Response_Stream
   ErrorLabel (errHandler2)
   If FileExist(File) Then imgCaptured.Image=File
   sub_my_stream
   Return
   
   errHandler2:
      sub_my_stream
End Sub

Sub sub_my_stream
   If isCanceled=False Then
      tmrCurent.Enabled=False
      lblPgTotal.Text=Round(nextRow/tblCap.RowCount*100)&" %"
      If nextRow<tblCap.RowCount Then
         curNr=tblCap.Cell("Column1",nextRow)
         nextRow=nextRow+1
         pgBarTotal.Value=nextRow
         lblPgTotal=nextRow&" of "&pgBarTotal.Maximum
         url=beforeNumber&curNr&afterNumber
         DownLoadFile(url)
      Else
         If tblFail.RowCount>0 Then
            tblFail.SaveCSV("FailedImage.csv",",",False)
         End If
         Msgbox("End Of Capturing")
         tmrCurent.Enabled=False
      End If
   End If
End Sub

Sub fgGradButtonStart_Click
   sub_my_stream
   fgGradButtonStart.Enabled=False
End Sub

Sub fgGradButtonCancel_Click
   isCanceled=True
   Response.CancelStream=True
   tmrCurent.Enabled=False
End Sub

Sub tmrCurent_Tick
  lblPgCurent.Text=Round(response.DownloadedBytes / 1000)&" of " & total
   pgBarCurent.Value=Round(response.DownloadedBytes / 1000)
End Sub

But when I run it somtimes work sometimes doesn't work because the response code on the same element, sometimes 200 sometimes other?

I understrand the asyncresponse but i don't know how can i add to this a looping function what download all of url on my list? :sign0085:
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…