Semen Matusovskiy Well-Known Member Licensed User Mar 18, 2018 #1 Hi, guys -- I found very old topic https://www.b4x.com/android/forum/threads/threading-library.6775/#content . Does B4A include built-in tools to lock processes ? My app executes concurrent downloads and I need to lock a place, which controls access/refresh tokens PS. I tried the Lock object from threading library and my own method, using variables. No success. Probably, a reason is relative to CallSub2, Wait For or Sleep. The construction is following. Activity calls a subroutine ("RefreshTokens") inside service (using CallSub2). "RefreshTokens" tries to lock itself at the begining and to unlock, when done. Last edited: Mar 18, 2018
Hi, guys -- I found very old topic https://www.b4x.com/android/forum/threads/threading-library.6775/#content . Does B4A include built-in tools to lock processes ? My app executes concurrent downloads and I need to lock a place, which controls access/refresh tokens PS. I tried the Lock object from threading library and my own method, using variables. No success. Probably, a reason is relative to CallSub2, Wait For or Sleep. The construction is following. Activity calls a subroutine ("RefreshTokens") inside service (using CallSub2). "RefreshTokens" tries to lock itself at the begining and to unlock, when done.
Erel B4X founder Staff member Licensed User Longtime User Mar 19, 2018 #2 Don't use the threading library. It will only cause problems. Assuming that you are using OkHttpUtils2 then downloads are already executed asynchronously with a pool of threads. JobDone is raised on the main thread. Upvote 0
Don't use the threading library. It will only cause problems. Assuming that you are using OkHttpUtils2 then downloads are already executed asynchronously with a pool of threads. JobDone is raised on the main thread.
Semen Matusovskiy Well-Known Member Licensed User Mar 19, 2018 #3 In my case : 1) In main activity CallSub3 (ServiceUpload, "subRefreshTokens", ...) ' Then waits (using variables), when subRefreshTokens will be done 2) In ServiceUpload Sub subRefreshTokens '<--- Here I need to lock httpjobHttpJob.PostMultipart ... Wait For (httpjobHttpJob) JobDone (httpjobHttpJob As HttpJob) '<-- Here I need to unlock End Sub How can I lock/unlock ? Upvote 0
In my case : 1) In main activity CallSub3 (ServiceUpload, "subRefreshTokens", ...) ' Then waits (using variables), when subRefreshTokens will be done 2) In ServiceUpload Sub subRefreshTokens '<--- Here I need to lock httpjobHttpJob.PostMultipart ... Wait For (httpjobHttpJob) JobDone (httpjobHttpJob As HttpJob) '<-- Here I need to unlock End Sub How can I lock/unlock ?
KMatle Expert Licensed User Longtime User Mar 19, 2018 #4 Just an idea - it's bad to fire a lot of jobs against a server (might be seen as an attack) - how many jobs are we talking about? (10 isn't worth it, 1000 -> why?) - an Android device is not a server, where are these "many" jobs come from? - You can pack the data to one job by using a List with maps containing all the data (php or another component will handle it on the server) - So you just send "one" job with all the data - I first started to send x jobs, too. I got into troubles so now I'm with lists/maps which is absolutely reliable (maps can contain anything) Upvote 0
Just an idea - it's bad to fire a lot of jobs against a server (might be seen as an attack) - how many jobs are we talking about? (10 isn't worth it, 1000 -> why?) - an Android device is not a server, where are these "many" jobs come from? - You can pack the data to one job by using a List with maps containing all the data (php or another component will handle it on the server) - So you just send "one" job with all the data - I first started to send x jobs, too. I got into troubles so now I'm with lists/maps which is absolutely reliable (maps can contain anything)
Erel B4X founder Staff member Licensed User Longtime User Mar 19, 2018 #5 Semen Matusovskiy said: How can I lock/unlock ? Click to expand... You don't to lock anything as your code always runs on the main thread. Upvote 0
Semen Matusovskiy said: How can I lock/unlock ? Click to expand... You don't to lock anything as your code always runs on the main thread.
Semen Matusovskiy Well-Known Member Licensed User Mar 19, 2018 #6 Erel -- Like Windows developer I obviously do not understand how Android processes events/messages. I did following "lock". Do While flagBusy = True Sleep (10) Loop flagBusy = True Log ("Enter") .... Wait For .... flagBusy = False Log ("Exit") flagBusy is Process_Globals variable. In log file I see "Enter", "Enter", "Exit", "Exit". How Anfroid bypasses similar lock ? ----------------------------- KMatle -- For example, one request is relative to information, which user sees on the screen, when activity starts. Another to Navigation Drawer, which I will show later. Of course, it's possible to download files step by step. But I want to open drawer asap. Last edited: Mar 19, 2018 Upvote 0
Erel -- Like Windows developer I obviously do not understand how Android processes events/messages. I did following "lock". Do While flagBusy = True Sleep (10) Loop flagBusy = True Log ("Enter") .... Wait For .... flagBusy = False Log ("Exit") flagBusy is Process_Globals variable. In log file I see "Enter", "Enter", "Exit", "Exit". How Anfroid bypasses similar lock ? ----------------------------- KMatle -- For example, one request is relative to information, which user sees on the screen, when activity starts. Another to Navigation Drawer, which I will show later. Of course, it's possible to download files step by step. But I want to open drawer asap.
DonManfred Expert Licensed User Longtime User Mar 19, 2018 #7 Please use [CODE]code here...[/CODE] tags when posting code. Please upload a small project which shoes the issue Also check this tutorial https://www.b4x.com/android/forum/threads/b4x-resumable-subs-sleep-wait-for.78601/ Last edited: Mar 19, 2018 Upvote 0
Please use [CODE]code here...[/CODE] tags when posting code. Please upload a small project which shoes the issue Also check this tutorial https://www.b4x.com/android/forum/threads/b4x-resumable-subs-sleep-wait-for.78601/
Semen Matusovskiy Well-Known Member Licensed User Mar 19, 2018 #8 It seems to me, I found the solution. It's necessary to check "busy"-flag inside Activity, not inside the service. Service: Code: Copy Sub Process_Globals Public booleanBusy As Boolean End Sub Sub RefreshTokens '--- Protected --- '------------------------ booleanBusy = False End Sub In Activity: Code: Copy ' Wait ready status Do While (MyService.booleanBusy) Sleep (10) Loop ' Occupy and call MyService.booleanBusy = True CallSub (MyService, "RefreshTokens") ' Wait ready (means Done) Do While (MyService.booleanBusy) Sleep (10) Loop Thanx for replies Last edited: Mar 19, 2018 Upvote 0
It seems to me, I found the solution. It's necessary to check "busy"-flag inside Activity, not inside the service. Service: Code: Copy Sub Process_Globals Public booleanBusy As Boolean End Sub Sub RefreshTokens '--- Protected --- '------------------------ booleanBusy = False End Sub In Activity: Code: Copy ' Wait ready status Do While (MyService.booleanBusy) Sleep (10) Loop ' Occupy and call MyService.booleanBusy = True CallSub (MyService, "RefreshTokens") ' Wait ready (means Done) Do While (MyService.booleanBusy) Sleep (10) Loop Thanx for replies