B4A Library Threading library

This library lets you run Basic4android Subs on separate threads to the main GUI thread. It also contains a Lock (semaphore) object that can be used for resource access arbitration and thread synchronisation.

Included is also an enhanced Exception object to give more information on any Exception caught. This can also rethrow those caught Exceptions and can create new Exceptions to throw.

EDIT :- Version 1.1 posted. See post #13 for details.
 

Attachments

  • Threading1.1.zip
    19.2 KB · Views: 7,494
Last edited:

kiki78

Active Member
Licensed User
Longtime User
Thank you Erel, I use this workaround for future.

As I'm not easy with Eclipse and Java, may I use B4A "Compile To Library" for my "threading" library ?

Regards
 

kentco

Member
Licensed User
Longtime User
Hi

I have a problem, i am using the threading libary and for some reason i am now starting to get an error on the device. Error Occurred An error has occurred in sub: java.lang.runtimeexception: Thread.Start : Sub Start_Send not found!

The call to the sub is correct as it has worked. I have gone back to another programme and this has stopped working too. if i take Thread.start(null,"Start_Send", Array As Object(SP_val)) and just use Start_Send the routine works. The issue is on the device when using the threading i get the sub is not found.

Anyone have any ideas? or can point me in the right direction

Thanks
Colin
 

kentco

Member
Licensed User
Longtime User
Ok, i think i have found out why if i compile with release only it works if i compile with release and obfuscated it crashes. Is this a known bug?

Thanks
Colin
 

kentco

Member
Licensed User
Longtime User
Hi Erel
Yes it is the name, i have tried it with and without the _ I don't know if it is important but if the name is StartSend the error is sub not found sendstart all lowercase i also created a lower case sub to see if it would pick it up but still get the error. if you call the sub without the thread it works so i guess the name is correct
 

kentco

Member
Licensed User
Longtime User
ok Erel i have attached a simple sample, if it is compiled with release only it works if it is compiled with release and obfuscated it errors. i have taken all the underscores out to confirm it is not them causing the problem
 

Attachments

  • Tread sample.zip
    6.8 KB · Views: 366

kentco

Member
Licensed User
Longtime User
Informatix, I did read them and I looked at the pitfalls page, I then found this bit of code and explanation and went down the wrong route!

B4X:
CallSub("Main", "Test1") 'Works because it is a static string (Test sub will not be renamed)
Sub Test1
Log("a")
End Sub

I had wrongly assumed my code used a static string to identify the subroutine.
My code
B4X:
PlayTH.Start(Null, "testTH",Null)

What i did not realize is that the "testTH" although being a static string within the call to setup the tread it is still a non-static string within the tread library and is there for Skipped at runtime because the sub is renamed hence the underscore is not optional but most defiantly required. I had assumed from Erel's post that using and underscore should work if i wanted it to hence why i took it out. I now know better.................


Colin
 

Informatix

Expert
Licensed User
Longtime User
Informatix, I did read them and I looked at the pitfalls page, I then found this bit of code and explanation and went down the wrong route!

As a general rule of thumb, I try to put an underscore in all function names, so I don't finish the day with a headache trying to understand if I need the underscore or not. ;) Same thing for my file names: all in lower case.
 

andrewj

Active Member
Licensed User
Longtime User
Hi,
I'm having a small problem with the ExceptionEx object. I have recursive file copy and delete operations, and I want to be able to cancel them at any time. I've put the following code in at the appropriate point:

B4X:
Dim ex As ExceptionEx
If iRet = DialogResponse.CANCEL Then
            ex.Initialize("Copying cancelled")
            ex.Throw
End If

Then in my local "Catch" clause I have the following:

B4X:
Catch
    If ex.Message = "File Skipped" Then
        Log("File copy skipped as requested")
    Else If ex.Message = "Copying cancelled" Then
        ex.Throw                                    ' Re-raise for handling at next level
    Else
        Msgbox(LastException.Message, "Error Occurred")
    End If
End Try

This does re-raise the error for handling at the next level up, but doesn't correctly set the message. If raised only one level down, then in the top-level handler I get the error "java.lang.Exception: java.lang.Exception: Copying cancelled". If raised more than one level down I just get a generic "InvocationTargetException" error.

Any idea how I can get the chosen message to propagate correctly?
Thanks
Andrew
 

andrewj

Active Member
Licensed User
Longtime User
Sorry Erel, I need a bit more help. Where do I put that code - in the catch block of the lower-level module, or in the top-level one? I don't seem to be able to get anything other than an "InvocationTargetException" in the top routine.
Thanks
Andrew
 

andrewj

Active Member
Licensed User
Longtime User
The overall code is quite complicated, but I think the core is in the exception handlers of two routines:

Every level in the recursion has a catch block like the following:
B4X:
Catch
    If ex.Message = "File Skipped" Then
        Log("File copy skipped as requested") ' I just want to dump this exception and 
                                      ' carry on with the next file, this seems to work
    Else If ex.Message = "Copying cancelled" Then
        ex.Throw  ' I want to re-raise for handling at next level up, cancelling all further processing
    Else
        Msgbox(LastException.Message, "Error Occurred")
    End If
End Try

The top level routine in the Activity has the following catch block:
B4X:
Catch
    If LastException.Message.Contains("Copying cancelled") Then
        Msgbox("Copy operation cancelled", "Confirmation")  ' This never triggers
    Else
        Msgbox(LastException.Message, "Error Occurred") ' This doesn't contain the "copying cancelled" text inserted lower down
    End If
End Try

Does this make sense?
Thanks
Andrew
 
Top