r/tasker • u/dougwzz99 • Oct 26 '26
Task collision and single threading
Hi,
I have a profile that intercepts notifications from an app and fires off a task to process the notification contents.
The app in question can issue several notifications at the same time. How can I ensure that the triggered task is single threaded, with only one task instance running at a time while at the same time getting the correct information from the notification that it is processing.
There is an option in task settings to 'Run Both Together' but I don't want the task activity to be interlaced and anyway there could be more than 2 instances at play here. I'm looking at setting up a lock/unlock gate mechanism at the start and end of the task, but not sure of the exact best way to approach this (or if there is another way to guarantee task single threading).
Appreciate any advice.
TIA.
2
u/The_IMPERIAL_One S24 Ultra (A16) | realme GT NEO 3 (A14) Oct 26 '26
Collision Handling > Abort New Task
Does this not work? Or is it not sufficient?
2
u/DylanXu054728 Oct 27 '26
Abort New Task on its own will drop the data from the 2nd and 3rd notification, since those task instances just get killed. What you want is a queue: the profile task only records the notification, and a separate task consumes the queue one item at a time.
Profile task (Enqueue), collision Run Both Together:
Process Queue task, collision Abort New Task:
Every notification gets pushed, and only one Process Queue ever runs. If it's already running, the new Perform Task is aborted but the loop still picks up the item because it re-checks the array before exiting.
Notes: the queue has to be global (capital letter in the name) so both tasks see it. There's a tiny window where an item is pushed right after the final Goto check; if that matters, add Wait 1 second and the same check once more before the task ends.
1
u/Exciting-Compote5680 Oct 26 '26
As far as I know tasks are performed in a single queue ('Run Both Together' just means the second instance will run after the first is completed, assuming they are running with equal priority). What works best for your use case depends mostly on the specific situation/information/notifications, or whatever 'the correct information' means for your use case (first notification? last notification?). The issue you can run into with 'Run Both Together' is that the number of queued tasks exceeds the maximum queue length and any task over the limit is ignored. If it is just by a couple (and the tasks are short/fast) you could increase that number in Preferences. But using 'Abort New Task' or 'Abort Existing Task' always means you are ignoring some notifications. If that works for your use case (for instance because the latest notification always has the most recent and complete information) you have less risk of clogging the queue. If you think you need some sort of mechanism, you could perhaps look at
%TRUN("A comma-separated list of any named tasks which are currently running. The list always starts and ends with a comma to make matching easier, if it's not empty."). But I think you are going to run into the same tradeoff: either you have a lot of notifications to process, or you discard some of them.1
u/Rich_D_sr Oct 27 '26
Give this a read..
https://www.reddit.com/r/tasker/s/hioeA9UKdf
1
u/sasreedit S22, GW5P Nov 01 '26
A different thought here... Are the notifications received all the same title & text? If no, maybe identify which is fired first and simply react to that notification only.
You can discover by using AN and a text file to see all the notifications.