B4J Question "Wait For " interesting behaviour

j_o_h_n

Active Member
Licensed User
Hi
I have seen what I think is instructive behaviour with this when doing things the way that
you are not supposed to! That is, when you are not using a sender filter.

In the code below, as you'd expect, imageview1 never gets a bitmap loaded. But what i found
surprising is that, more often than not, the image that is loaded into imageview2 is the one
that was supposed to end up in imageview1.
This suggests to me that the the job variable referenced after the wait for line in the
Sub DownloadFile can be different to the one declared in the same sub!
It really underlines the importance of the sender filter which would prevent this from
happening.

B4X:
Sub DownloadFile (webaddress As String, iv As ImageView)
    Dim job As HttpJob
    job.Initialize("", Me)
    job.Download (webaddress)
    wait for jobdone(job As HttpJob)
    place = place + 1
    Log(place)
    iv.SetImage (job.GetBitmap)
End Sub

Sub Button1_Click

    place = 0
    ImageView1.SetImage(Null)
    ImageView2.SetImage(Null)
    ImageView3.SetImage(Null)
 
 
    DownloadFile(strEiffelTower, ImageView1)
    DownloadFile(strArcDeTriomphe, ImageView2)
End Sub

Sub jobdone(job As HttpJob)
    place = place + 1
    Log(place)
    ImageView3.SetImage(job.GetBitmap )
End Sub


The other thing I noticed was that if you also had a Sub JobDone present then that would
capture whichever of the jobDone events that wasn't captured by Wait For.
Wait For
will always have precedence over Sub jobdone but only for the raising of a single event.

If anything above is a misconception on my part I'd be very grateful to be put right!
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
Try
B4X:
Sub DownloadFile (webaddress As String, iv As ImageView) as ResumableSub
    Dim retVal as Boolean
    Dim job As HttpJob
    job.Initialize("", Me)
    job.Download (webaddress)
    wait for jobdone(job As HttpJob)
    If job.Success Then
        place = place + 1
        Log(place)
        iv.SetImage (job.GetBitmap)
    Else
        Log("Error: " & job.ErrorMessage)
    End If
    retVal = job.Success
    'Release job when done!
    job.Release
    return retVal
End Sub

Sub Button1_Click

    place = 0
    ImageView1.SetImage(Null)
    ImageView2.SetImage(Null)
    ImageView3.SetImage(Null)
 
 
    Wait For (DownloadFile(strEiffelTower, ImageView1)) Complete (success as Boolean)
    Wait For (DownloadFile(strArcDeTriomphe, ImageView2)) Complete (success as Boolean)
End Sub
This should fix your image view issue. Also, do not mix Wait For with JobDone routine. Do one or the other (preferably Wait For). Not releasing the job can also cause issues.
Note: Code not tested for syntax/logical errors (I just modified your code here on the forum).
Note2: Code updated as per @j_o_h_n's post below
 
Last edited:
Upvote 0

j_o_h_n

Active Member
Licensed User
Thanks Oliver, you did a good job of modifying it on the fly then. I loaded it up and it worked, (just needed to put extra parentheses on the two calls at the bottom because the compiler was complaining about parameter name hiding a global variable name).
(Actually I knew my code wasn't good I'm just experimenting with resumable subs at the moment.)
 
Upvote 0
Top