Stop a loop with a button?

N1c0_ds

Active Member
Licensed User
I am trying to insert a "cancel" button to stop a download while it's in the download loop (using the downloadfile method with the HTTP lib) but the UI is frozen until a download is completed.

Any idea of how I could do this?
 

Cableguy

Expert
Licensed User
Longtime User
yes, use the new asyncronous method, wich runs in its own thread inside the dll...
That way you can still interact with the forms controls...
To stop th download just set the response to an emty string...
 

N1c0_ds

Active Member
Licensed User
yes, use the new asyncronous method, wich runs in its own thread inside the dll...
That way you can still interact with the forms controls...
To stop th download just set the response to an emty string...

Alright, I'll try that.

Now how can you check for a connection's validity with the asynchronous method? I already use it to silently download screenshots but I don't know how to check for an error.

I currently use "NoConnection", a subs that returns "false" if an error occurs while getting the response, but I don't know how to implement this on an async. call.
 

Cableguy

Expert
Licensed User
Longtime User
from the lattest http dll help file:

The Response event is raised after a call to GetAsyncResponse and only after receiving the respond from the server.
You should check the value of ResponseCode before fetching the response stream to make sure that the response is valid.
 

N1c0_ds

Active Member
Licensed User
Here's the code I use now to save .cab files. How can I stop it with a cancel button? (let's say floadingcancel, a softkey button)

B4X:
#Region SaveCab
Sub SaveCab(URL,InstallAfterDownload)
Install=InstallAfterDownload

   If Prefs.AskForSaveLocation=True Then
      If SaveDialog.Show<>cCancel Then
         FilePath=SaveDialog.File
      Else
         Return
      End If
   Else
      If DirExist(AppPath & "\temp")=False Then DirCreate(AppPath & "\temp")
      FilePath=AppPath & "\temp\application.cab"
   End If
   
   WaitCursor(True)
   
      If Checkconnection(URL)=False Then
         NoConnection(True,False)
         WaitCursor(False)
         Return
      End If
      
   Response.New1
   Request.New1(URL)
   Request.UserAgent="Gecko/Beta (http://getgecko.org)"
   Request.GetAsyncResponse
End Sub

Sub Request_Response
   If Request.ResponseCode=200 Then
      Response.Value=Request.AsyncResponse 'Launch the request and get the response.
      Response.GetAsyncStream(Filepath) 'Get the stream
   Else
      WaitCursor(False)
      NoConnection(True,False)
   End If
End Sub

Sub Response_Stream
WaitCursor(False)
Retry:
   If Install=True Then
      ShellAndWait("wceload.exe", Chr(34) & Filepath & Chr(34))
      If Msgbox("Did the application install successfully?","Installation",cMsgboxYesNo,cMsgboxQuestion)=cNo Then Goto Retry
   End If
FrmLoading.Close
End Sub
#End Region
 

joel2009

Member
Licensed User
i'm not as farmiliar with the http library, but you could add an if value to check if a check box is checkd or not, and exit if it is. then set the buttons to change the value of the check box too true (and of course reseeet it to false on download button click)
 

N1c0_ds

Active Member
Licensed User
Something like:
B4X:
Sub btnStop_Click
 Response.CancelStream = true
End Sub
This button should be disabled by default, and only enabled after the call to GetAsyncStream.
You could also use Request.DownloadedBytes to show the progress to the user.

Both things work perfectly! Thanks a lot!

I use a timer's tick event to refresh the loading bar. The progressbar's value is Response.Downloadedbytes and its maximum is Response.Contentlenght.

:sign0162:
 
Top