Waiting for an event/callback

Hello guys, the problem is simple to describe: I have a init procedure that needs to read a file to preset the content of a TinyDB. It is simple but reading the file is in the middle of the init procedure so actually the real "init" procedure is splitted in init itself and in FileSetup.GotText. OK, it works but it is not so nice to see and just in case I would need to read 2 files I would split the "init" procedure in 3 different blocks.
So, I was thinking if there is some sort of wait-event already implemented or an extension able to do that consiering that is not possible to use a wait loop inside an event to wait something that happens on another event.

use the following logic:

  1. do some things
  2. set a global variable to 1 and read file 1
  3. in the File.GotText event if global variable = 1 then set the global variable to 2 and read file 2
  4. in the File.GotText event if global variable = 2 then end

it might be, that I did not understand, what you are trying to do... in this case it would help very much to provide a screenshot of your relevant blocks...


OK, in this way the init procedure is always splitted in no more than 2 part (the init procedure and the GotText callback) for any number of files. What I would like to do is to have a init procedure that starts to read a file and then waits until the file is read. Seems that reading a file is executed in a different thread so to synchronize the program flow we should need some sort of synchronization primitive. In practice I am asking if we have a sort of semaphore, it is a simple concept of a task put in wait itself until another one completes its job and it has to signal that completed its job.

If the files were read on the same thread then the UI would hang while reading the files. I don't know if it would be nice for the user of your app.

A simple example of how to read multiple files from an asset:

read_files.aia (3.3 KB)

The app reads all 5 files one by one and displays the read text in the list view. Where there are empty conditions, you can add events for each read file.

So we don't need wait routines that stop us from the user interface.

1 Like

see also this example https://puravidaapps.com/filebyfile.php, which demonstrates how to work with asynchronic methods