Android Question "Sleep" conflicts with "Exit" ?

Colin Harvey

Member
Licensed User
Hi, I had a procedure that included a Sleep(0) statement as a short pause in a loop that moved an Imageview around on screen.

I recently tried to add an "IF <condition> Then Exit" statement to this procedure so as to drop out of a Select Case statement, but I get an error message: "Object Reference not set to an instance of an object".

Why is this a problem ? Something to do with Resumable subs ? My code was of the following general form:

Select Case variable
Case
Case
some code
If <condition> THEN EXIT <<< Adding this line generates the error
more code​
Case Else
For ... To ...
'move Imageview
Sleep(0) <<< Removing this line removes the error​
Next​
End Select
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi, I had a procedure that included a Sleep(0) statement as a short pause in a loop that moved an Imageview around on screen.

I recently tried to add an "IF <condition> Then Exit" statement to this procedure so as to drop out of a Select Case statement, but I get an error message: "Object Reference not set to an instance of an object".

Why is this a problem ? Something to do with Resumable subs ? My code was of the following general form:

Select Case variable
Case
Case
some code
If <condition> THEN EXIT <<< Adding this line generates the error
more code​
Case Else
For ... To ...
'move Imageview
Sleep(0) <<< Removing this line removes the error​
Next​
End Select
Can you post the actual code?

- Colin.
 
Upvote 0

Colin Harvey

Member
Licensed User
Any code of that general form causes the same problem, my original is 650 lines long (!) so I wont post here, but even the following short example has exactly the same issue, so it seems to be a generic problem combining EXIT statements with Resumable Subs (ie. any procedure containing a Sleep statement):

B4X:
Sub TestProc(intTest As Int, intParam As Int)
  
    Select Case intTest
        Case 0 - 5
            intParam = intParam * 2
        Case 6 - 10
            intParam = intParam * 5
            If intTest = 10 Then Exit
            intParam = intParam * 2
          
        Case Else
            For nxMove = 0 To 100 Step 10
                Sleep(0)
            Next
    End Select
      
End Sub
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I have several resumable subs in apps that also have Exit statements in them & I don't have any issues - however none of them are using Exit in a Select statement (they are all in For loops)...

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Fyi - there's an issue with your Select Case statement. Unless you want your first case to be -5 & your second one to be -4, it won't work. If you are looking for ranges, you'll have to use:

B4X:
Sub TestProc(intTest As Int, intParam As Int)
    Select Case intTest 
        Case 0, 1, 2, 3, 4, 5
            intParam = intParam * 2
        Case 6, 7, 8, 9, 10
            intParam = intParam * 5
            If intTest = 10 Then Exit
            intParam = intParam * 2
        Case Else
            For nxMove = 0 To 100 Step 10
                Sleep(0)
            Next
    End Select
End Sub

Btw - I ran a test using Exit & Sleep(0) in a Case statement & had the same issue, so it seems there might be a bug in B4A - or perhaps there's a logical reason why you can't use them both...

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
You can use Return instead of Exit. The way your example code is written, Return should work OK, because there's nothing after the Select Case block that would need to be executed in the case of an Exit.

- Colin.
 
Upvote 0

Colin Harvey

Member
Licensed User
Thanks for replying, its a relief to know its not just me !
Apologies for the poor quality of my example (ie. the range was misleading).
However, in my "real" 650 line code though, the EXIT does drop out of the select block to do more work, so I have to move the Sleep(0) statement out of my procedure.

Just tested this, and if I remove the Sleep(0) to another procedure, everything is fine ! (ie. the Sleep and the Exit are no longer present in the same procedure)

B4X:
Sub TestProc(intTest As Int, intParam As Int)
    Select Case intTest
        Case 0, 1, 2, 3, 4, 5
            intParam = intParam * 2
        Case 6, 7, 8, 9, 10
            intParam = intParam * 5
            If intTest = 10 Then Exit
            intParam = intParam * 2
        Case Else
            For nxMove = 0 To 100 Step 10
                DoTheSleep
            Next
    End Select
End Sub

Sub DoTheSleep
    Sleep(0)
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Just tested this, and if I remove the Sleep(0) to another procedure, everything is fine !
The sleep call doesn't do anything (useful) in this case. It will not have any effect on TestProc execution.

You can easily remove the Exit and replace it with:
B4X:
If Not(<condition>) Then
 ...
End If
Case NextCaseHere

There is a bug here. It will be checked.
 
Upvote 0
Top