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:

Sberla

Active Member
Licensed User
Longtime User
I'll do everithing you say, but hte thread still doesn't want to start, can someone please send me a code that works? so i can see what is my mystake.. thakns
 

kiki78

Active Member
Licensed User
Longtime User
May you post your test code ? Perhaps we can found problem in it.
Don't forget to compile in Release mode as Threading library not work fine in Debug mode.
When your threaded code work fine, it is recommended to put it in library.

Regards
 

Sberla

Active Member
Licensed User
Longtime User
I try in release mode either.. nothing to do, can the library doesn't works because my version isn't the latest one? the version i use is the 3.82
 

kiki78

Active Member
Licensed User
Longtime User
I use this library since B4A version 2.71 without problem. So I think this is not your problem.
 

Sberla

Active Member
Licensed User
Longtime User
This is the expample of threading,please explane to me why it doesn't works.. i'm in your hands ;)
 

Attachments

  • Threading.zip
    494.4 KB · Views: 340

kiki78

Active Member
Licensed User
Longtime User
Thread start correctly on my tablet !
Do you test it on real device ?
 

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
I download the zip, extract, compile and when press start I get an Exception: wrong nuember of arguments; expected 4, got 0
Cant test it.
 

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
I'm trying to test this lib but I got errors with variables dont defined like End1, ok, msg.
Am I missing somethig?
 

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
How can I use/modify this example in the case of a service started from the main sub?
I want the service running in another thread.
Thanks
 

stari

Active Member
Licensed User
Longtime User
Threading problem
Hello, i have trayed Threading.zip but i get error:
B4X:
PackageAdded: package:anywheresoftware.b4a.agraham.threaddemo
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_btnstart_click (java line: 402)
java.lang.RuntimeException: Thread.Start : Sub threadsub1 not found!
    at anywheresoftware.b4a.agraham.threading.Threading.Start(Threading.java:207)
    at anywheresoftware.b4a.agraham.threaddemo.main._btnstart_click(main.java:402)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:3511)
    at android.view.View$PerformClick.run(View.java:14110)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4430)
    at java.lang.reflect.Method.invokeNative(Native Method)

What can i do ?
 

walterf25

Expert
Licensed User
Longtime User
Hello All, i have been using this Threading library in a project i have been working on for a while, i just noticed something very strange, and i would like to know if anyone else has came across this issue or has anyone else even noticed it.

Here is the scenario, the app i'm working basically allows the user to select multiple images from their image gallery/camera etc... once the user selects all the images he/she intends to use then those images are stitched up together to make a video.

I'm using the threading library to display a circle progress library i created to show the current progress of the image/video processing. I know for a fact that it is not my circle progress bar library causing the issue as i have tried it in other projects and it works just fine.

The problem i'm seeing is that if I compile the app in Release (obfuscated mode) i receive this error after i select the images and the circle progress bar should appear and start showing the progress.

LogCat connected to: LGLS99055703165
--------- beginning of system
--------- beginning of main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
thumbtag :/storage/emulated/0/DCIM/Camera
** Activity (main) Pause, UserClosed = false **
** Activity (multipleselect) Create, isFirst = true **
** Activity (multipleselect) Resume **
** Activity (multipleselect) Pause, UserClosed = false **
** Activity (multipleselect) Resume **
item selected: /storage/emulated/0/DCIM/Camera/20150627_221602_HDR.jpg Items Selected: 1
item selected: /storage/emulated/0/DCIM/Camera/20150627_221503_HDR.jpg Items Selected: 2
item selected: /storage/emulated/0/DCIM/Camera/20150625_145959.jpg Items Selected: 3
item selected: /storage/emulated/0/DCIM/Camera/20150625_145947.jpg Items Selected: 4
item selected: /storage/emulated/0/DCIM/Camera/20150627_232122_HDR.jpg Items Selected: 5
item selected: /storage/emulated/0/DCIM/Camera/20150629_214241_HDR.jpg Items Selected: 6
item selected: /storage/emulated/0/DCIM/Camera/20150702_113902_HDR.jpg Items Selected: 7
item selected: /storage/emulated/0/DCIM/Camera/20150703_110238_HDR.jpg Items Selected: 8
item selected: /storage/emulated/0/DCIM/Camera/20150703_110247_HDR.jpg Items Selected: 9
item selected: /storage/emulated/0/DCIM/Camera/20150703_110254_HDR.jpg Items Selected: 10
item selected: /storage/emulated/0/DCIM/Camera/20150703_113027.jpg Items Selected: 11
item selected: /storage/emulated/0/DCIM/Camera/20150703_123614_HDR.jpg Items Selected: 12
** Activity (multipleselect) Pause, UserClosed = true **
onActivityResult: IOnActivityResult was released
** Activity (main) Resume **
number of images selected is: 12
** Service (processvideo) Create **
** Service (processvideo) Start **
processvideo_service_start (java line: 137)
java.lang.RuntimeException: Thread.Start : Sub threadsub1 not found!
at anywheresoftware.b4a.agraham.threading.Threading.Start(Threading.java:207)
at com.genesis.videomaker.processvideo._service_start(processvideo.java:137)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at com.genesis.videomaker.processvideo.handleStart(processvideo.java:71)
at com.genesis.videomaker.processvideo.onStartCommand(processvideo.java:55)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2887)
at android.app.ActivityThread.access$2100(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
java.lang.RuntimeException: Unable to start service com.genesis.videomaker.processvideo@118a522e with Intent { cmp=com.genesis.videomaker/.processvideo }: java.lang.RuntimeException: java.lang.RuntimeException: Thread.Start : Sub threadsub1 not found!
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2904)
at android.app.ActivityThread.access$2100(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Thread.Start : Sub threadsub1 not found!
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at com.genesis.videomaker.processvideo.handleStart(processvideo.java:71)
at com.genesis.videomaker.processvideo.onStartCommand(processvideo.java:55)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2887)
... 9 more
Caused by: java.lang.RuntimeException: Thread.Start : Sub threadsub1 not found!
at anywheresoftware.b4a.agraham.threading.Threading.Start(Threading.java:207)
at com.genesis.videomaker.processvideo._service_start(processvideo.java:137)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
... 13 more
--------- beginning of crash

Now if i compile the project in just Release mode (not obfuscated) then everything runs just fine.
Is this an issue with the threading library or with B4A itself, i'm using the latest and greatest version of B4A version 5.02 (1).

Has anyone else experienced this? I know it is not a big deal since i can still run the project in Release mode, but i would like to obfuscate the code so that it makes it harder for anyone else to de-compiles and sees my code once i release my app.

here is the relevant code in my ProcessVideo service

B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
Private video As VideoMaker
Private thread1 As Thread
Dim lock1 As Lock

End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
Dim args(0) As Object
thread1.Initialise("thread1")
lock1.Initialize(True)
thread1.Name = "Thread 1"
thread1.Start(Null, "ThreadSub1", args)



End Sub

Sub Service_Destroy

End Sub

Sub ThreadSub1
DoEvents
video.Initialize("video", Main.thumbtag, Main.newl, 480, 800 )
'DoEvents
Dim count As Int
Dim params(1) As Object

Do While count < Main.newl.Size
count = count + 1
params(0) = count
ok = False
Do Until ok
thread1.RunOnGuiThread("Update1", params)
ok = lock1.WaitFor(10)
Loop

Loop


MultipleSelect.selectedpics.Clear
Main.newl.Clear
Log("finished processing video")

End Sub

Sub Update1(count As Int)
Log("Image number: " & count)
lock1.Unlock
End Sub

Sub Thread1_Ended(endedOK As Boolean, error As String) 'The thread has terminated. If endedOK is False error holds the reason for failure
    Log("thread ended: " & endedOK & " " & "Error: " & error)
    StopService("")
End Sub

Sub video_progress(progress As Int)
    Dim prog As Float
    'Log("list size: " & MultipleSelect.selectedpics.Size)
    prog = (100/(MultipleSelect.selectedpics.Size))
    Dim prg As Double
    prg = prog   
    CallSubDelayed2(Main, "Update_Progress", prg)
End Sub

attached is a screenshot of when the app runs fine if compiled in regular release mode.


Hope either the Author of this library or Erel can answer this post.

Thanks,
Walter
progress.png
 

stevel05

Expert
Licensed User
Longtime User
Sub name is getting obsfuscated, but the call to it is probably not as it's a string.

B4X:
thread1.Start(Null, "ThreadSub1", args)

You should probably use a sub name with an underscore so that it is not obsfuscated.
 

walterf25

Expert
Licensed User
Longtime User
Sub name is getting obsfuscated, but the call to it is probably not as it's a string.

B4X:
thread1.Start(Null, "ThreadSub1", args)

You should probably use a sub name with an underscore so that it is not obsfuscated.
Wow, that did it, thanks for your fast reply stevel05, it works now when compiled in Release(obfuscated) mode.

Thanks,
Walter
 

Nondzu

New Member
Licensed User
Longtime User
i need help.

i have error after compilation
"
Compiling code. (0.11s)
Compiling layouts code. (0.03s)
Sending data to remote compiler. Error
step: Convert byte code - dex.
A referenced library is missing: threading
"
or
"
step: Compiling generated Java code.
javac 1.7.0
src\anywheresoftware\b4a\agraham\threaddemo\main.java:312: error: package anywheresoftware.b4a.agraham.threading does not exist
public static anywheresoftware.b4a.agraham.threading.Threading _thread1 = null;
^
1 error
"
please help me
Thank you
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
i need help.

i have error after compilation
"
Compiling code. (0.11s)
Compiling layouts code. (0.03s)
Sending data to remote compiler. Error
step: Convert byte code - dex.
A referenced library is missing: threading
"
or
"
step: Compiling generated Java code.
javac 1.7.0
src\anywheresoftware\b4a\agraham\threaddemo\main.java:312: error: package anywheresoftware.b4a.agraham.threading does not exist
public static anywheresoftware.b4a.agraham.threading.Threading _thread1 = null;
^
1 error
"
please help me
Thank you
As said by the error, the Threading library is missing. You have to add the two library files into the folder of your additional libraries.
 

yonson

Active Member
Licensed User
Longtime User
I download the zip, extract, compile and when press start I get an Exception: wrong nuember of arguments; expected 4, got 0
Cant test it.
I am getting exactly the same error, can someone clarify what is causing this please? this is happening on the example packaged with the library, in debug mode, running on a Samsung Note 4.
 

stevel05

Expert
Licensed User
Longtime User
It is not possible to run apps that use the threading library in debug mode (once they are working, you can compile modules that do to a library then debug the rest of the app), does it run in release mode?
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

Does it support B4J ?
 
Top