Android Question Control Sub with asynchronous operations

focus330

Member
Licensed User
Longtime User
Problem: I call a sub that contains a File.copy(.....).
After calling the sub I load the file copied, but it seems that sometimes copy indicated into the sub isn't finished. I test with file.exists but then : what the most elegant solution ?
Example:

B4X:
Sub One
       ....do something
       callsub Two
       Sleep ( ???? )  <-- 1000 ? 10000 ? 100000 ? It seems impossible to decide a correct value
       If file.exists(File.dirinternal,"Odissea.txt") then
                   .........
      else
                  log("File not copied")
                 ????????
                
     end if
end sub

Sub Two
     .......do something
     file.copy(file.dirassets,"Book.txt",file.dirinternal."Odissea.txt")
end sub

I try this...

B4X:
Sub One
       ....do something
       callsub Two
       Wait for Timer1_Tick
       Timer1.enabled = false
       .... do somethink 
end sub

Sub Two
     .......do something
     file.copy(file.dirassets,"Book.txt",file.dirinternal,"Odissea.txt")
     .......do something
     do until file.exists(File.dirinternal,"Odissea.txt")
          log("Copy not finished")
          Sleep (500)
     Loop
     timer1.Inizialize("Timer1",1)
     timer1.enabled = true
end sub

sub Timer1_tick
     timer1.enabled = false
End sub[/CODE

It works but my question is : Are there other best solution to continue code only when the async func of a sub is finished ?
 

DonManfred

Expert
Licensed User
Longtime User
use wait for and raise a event from the 2nd sub (the event you are waiting for)
 
Upvote 0

focus330

Member
Licensed User
Longtime User
Quick !!!
I raised the Timer1_tick - Is this correct or do you mean something else ?
Thanks DonManfred.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

focus330

Member
Licensed User
Longtime User
Searching yesterday for a solution i didn't see this post. Now
I understand how to manage resumable subs. Very well
Thanks a lot Erel / @DonManfred

It seems useful also for a similar situation that is when I test from a secondary form a value defined and setted in Starter that gives me an error if starter isn't finished.

Regards
 
Upvote 0

focus330

Member
Licensed User
Longtime User
Sorry Erel I didn't reply to your question.
Yes I'm sure about file.exists test. The example is extracted from a code of an app that runs on thousands smartphones. It seems occurs only with very slow devices.
I don't know what happens. I suppose ( empirical ) that the file isn't already physically registered into the paste or into the past-map or something similar considering this an asynchronous operation. I fear also that the problem may be do with an incorrect use of DoEvents, but I'll substitute with' wait for' and 'sleep' that seem better.
 
Last edited:
Upvote 0
Top