Open Source • Background Tasks: Itoo 🚀

Hi, I am trying to read multiple machines. When I do this with my app, the app gets blocked until the reading of all machines is finished. That's why I'm trying to use 'Background Task' to implement something like a BackgroundWorker as in 'Visual Studio.net'. Initially, I tried to create a Background Task to get familiar with it. So far, the app does what it's supposed to, but I still have some questions.

  1. I pass addresses to the task and receive the responses from the BroadcastEvent. Is that correct? Although it works, I'm not sure if it's correct or if it's just working by chance.

  2. I receive a notification from the green circled block. Why? I don't need this notification. Can I suppress it?

Attached are the blocks I am talking about.

Thank you very much for the excellent work and the effort.

Hello @Pac-Man

It is not possible to suppress it unless you use the Create Task block instead of Create Process (which can only run your service for a few minutes).


I dont get the idea of what you are trying to do? Why do you want to store something, pass it on to the background service and get it back? I dont get whats happening nor what you want to do.

You may be confusing the extension with asynchronous operations or simply new-thread operations? Am I right? Because this extension, background tasks, is just to run tasks in background even while the app is turned off.

Is it possible to use an any component event in the Register-Event call? And if so are there any special considerations I'd have to make?

So you want to listen to all of the components that run in the background?
Currently the extension does not offer Any Event since generally limited number of components are used in background. And their events needs to be manually registered.

Hey, yea sorry for the late reply, but to just give some context I'm using the phone Call component to allow me to catch the number of incoming calls, the regular component incoming call event would only trigger with a specific number, the "any phoneCall" variant would trigger the event with any phone number calling, and would also return the phone number as a variable, I am looking to do this in the background, I've managed to get itoo to register regular events like clock1.Timer, but not something like clock.Timer / any clock.Timer. Which as u said is probably, because the extension doesn't support it. So yea thanks a lot for informing me!

Edit: Wait a second :sob:, I might've been mistaken about the phone call component, I just decided to go through the documentation one more time, and apparently yea the regular event does trigger with any phone number, which means I don't need to use an any phone call component, I can't believe I didn't catch this even though I've been using the phone call component for quite a long time now :sob:. This means that I should just be able to get by using the regular event, I'll go ahead and test things out, and see if everything works fine with this piece of information.

Edit2: Seems like I still can't get the event to trigger in the background, well here are my blocks that relate to the background process, I just can't figure out what's wrong.

To find it out, use a Notifier.LogInfo method to log something and use logcat to check

LogInfo(message)
Writes an information message to the Android log.

EDIT: what is JmlPhoneCall? Any link?

Taifun

Also you should change the two if statements into an if - else statement


Taifun

Okay!! I'll be sure to try out the Notifier.LogInfo method, and also to answer your question the JmlPhoneCall is an extension that allows you to answer or cancel incoming phone calls, here's a link to the thread where I got the extension!

Edit: I went ahead and managed to get the notifier and logcat working, no changes tho on getting the event to trigger, I would say tho that having that the show alert method is still more convenient for this simple use case as I am still just testing in UI, and am not really needing any detailed logging information.

Understandable! Will do that as it is more readable, and reduces repetition of blocks thanks!

Dio you like to show us an updated screenshot of your blicks?

If you want to really do tests in the background, then there is no UI available

Taifun

I just remembered, that I already was looking into this...

Taifun

Yea sure!


I also included the screen inisilisation block in this screenshot

Well fair enough :sob:, but in this case I'm pretty sure the foreground service still runs while I have the app open, so the event should trigger, and show the UI popup in app, and as for outside the app, I wouldn't really need something like a log since if the event does get triggered(which is during a phoneCall), the CallAccept method/function would be triggered, and it would accept the call, and I'd know that yes indeed the event got triggered, the notifier is just a little extra tid bit of info that might serve itself :person_shrugging:

So what are the conclusion of your tests? Where does it work or not work?

Well, to simply conclude, I just wasn't able to get the registerEvent to work with the phoneCall1.PhoneCallStarted event, everything else works fine, I did try it with the clock1.Trigger event as i said earlier, and things did work perfectly fine there.

Read again Detect that there is a call - #13 by Taifun - Discuss - Kodular Community why it does not work and what could be done to get it running

Taifun

Omg, I can't believe I missed this part of the thread :sob:, thanks a lot I'll make sure to read through it!

Yes indeed!!

I was able to solve my problem of needing immediate UI feedback from the Player component when running in the background by simply moving that code from the UI to the background :slight_smile: . So now the main "show" runs in the background service and the UI executes control via Itoo.CallBackgroundProcedure and the background process updates the UI via the implicit ui_xxxxx mechanism.

No need for any locks or while loops!

However I have encountered a problem when attempting to report a Screen1.ErrorOccurred event when it occurs in the background process. The procedure Screen1_ErrorOccurred is not called. From what I can see in logcat output, it looks like the procedure "null_ErrorOccurred" is being sought and, of course, not found. When I add the procedure null_ErrorOccurred to the project, it DOES get invoked...

Here is a test case made from ItooMusic. The error that is induced (when the KDFC URL is toggled so that it is in error) is "702" and is caught and reported by both the UI and background players. However, if null_ErrorOccurred is disabled/removed from the project, the background player does not report the error.

I have attached the test project as well as some logcat output and screen shots. Let me know if I have not sufficiently explained this.

The test case:
ItooMusic_440_null.aia (4.2 MB)
The blocks:

Both players can be started by the UI and will play the KDFC San Francisco, CA, public radio stream using a URL.

When "Toggle KDFC URL" is clicked the URL is intentionally corrupted and neither player works. They report the error 702 in the label between the two sets of controls.

UI Error:

Background Error:

HOWEVER, If the project is modified to disable/delete procedure null_ErrorOccurred, the UI player reports the error 702, but the background service does not.

Here is some logcat output showing what is happening - null_ErrorOcurred is not found:
null-notFound.txt (1.8 KB)

12-26 23:30:37.450  9034  9034 E Form    : Form null ErrorOccurred, errorNumber = 702, componentType = Player, functionName = Source, messages = Unable to prepare https://18323.live.streamtheworld.com:443/KDFCFM_S.
12-26 23:30:37.478  9034  9034 I ItooCreator: AEvent(ErrorOccurred=null) args [com.google.appinventor.components.runtime.Player@44804e8, Source, 702, Unable to prepare https://18323.live.streamtheworld.com:443/KDFCFM_S.] listener = xyz.kumaraswamy.itoo.Itoo$1@1dc8539
12-26 23:30:37.478  9034  9034 I ItooCreator: startProcedureInvoke: null_ErrorOccurred & -1
12-26 23:30:37.478  9034  9034 I ItooCreator: startProcedureInvoke: failed to find name(null_ErrorOccurred)

BTW, if "Toggle KDFC URL" is clicked again, the URL is repaired and both players work once again - no errors.

I need this functionality because the radio stream URLs I have used sometimes change - what works today my not work tomorrow :frowning:

For now, I can use the null_ErrorOccurred workaround... but I figured it would be something that could be fixed(?) in a future release...and, as always, I may not be understanding this correctly.

Does this make sense?

Kind regards,
Randal

1 Like

Hello Randal Andress, thank you for the error report, this is indeed a bug that needs to be addressed. I've noted it down and will be fixed in the new update.

No problem... workaround is easy. But I also am coding the event Player1_PlayerError. Is it likely to have a similar issue or is this just a Screen1 event issue?

-Randal

It is just a Screen1 event issue, since there's no actual Screen in background, the extension thus fails.