Trouble with delays

Hi, I have had a good search but can’t find the solution to my problem or how to implement it.

I have a process running in the background that I want to poll to determine that more than zero items match the criteria (I got this working). the problem is it locks up my app, I have tried

delay 1
and
do shell script “sleep 1”
in the forums people talk about performSelector:withObject:afterDelay: - but I have been trying for a couple of hours to get that working and I am just stuck, any ideas.



repeat while criteria > 0
      
say "running"

-- what delay code can I use :)

end repeat

Use something like this:

on checkProgress()
	-- check whatever here
	if stillWaiting then
		my performSelector:"checkProgress" withObject:(missing value) afterDelay:1.0
	else
		-- do your stuff
	end if
end checkProgress

Thanks so much that did it!

Amazing.

I find Apples documentation so impenetrable at times, really appreciate your help with this.

Polling is worst case and should be used only if you have no choice.

A better practice is to call a handler or send a notification in the background process

OK I spoke to soon, I have a feeling I’m misunderstanding what’s going on.

Basically I want it to keep checking until IsCount is 10.

This is my current code, but its not working,


on checkProgress:sender

        if IsCount < 10 then
            say "running"
            my performSelector:"checkProgress" withObject:(missing value) afterDelay:1.0
            else
            say "finished"
        end if
        
end checkProgress:

when I trigger checkProgress
I get this:

2019-08-03 12:32:30.149827+1000 background checker[32492] *** -[AppDelegate checkProgress]: unrecognized selector sent to object <AppDelegate @0x6000002979e0: OSAID(4) ComponentInstance(0x810000)>
2019-08-03 12:32:30.150054+1000 background checker[32492] [General] *** -[AppDelegate checkProgress]: unrecognized selector sent to object <AppDelegate @0x6000002979e0: OSAID(4) ComponentInstance(0x810000)>
2019-08-03 12:32:30.151015+1000 background checker[32492] [General] (
0 CoreFoundation 0x00007fff435672fd __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff6dc38a17 objc_exception_throw + 48
2 AppleScriptObjC 0x00007fff41886c7f -[BAObjectProto methodSignatureForSelector:] + 0
3 CoreFoundation 0x00007fff4350918f forwarding + 1485
4 CoreFoundation 0x00007fff43508b38 _CF_forwarding_prep_0 + 120
5 Foundation 0x00007fff4575d6fa __NSFireDelayedPerform + 411
6 CoreFoundation 0x00007fff434d0060 CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 20
7 CoreFoundation 0x00007fff434cfc0c __CFRunLoopDoTimer + 851
8 CoreFoundation 0x00007fff434cf752 __CFRunLoopDoTimers + 330
9 CoreFoundation 0x00007fff434b0962 __CFRunLoopRun + 2130
10 CoreFoundation 0x00007fff434afebe CFRunLoopRunSpecific + 455
11 HIToolbox 0x00007fff4270f1ab RunCurrentEventLoopInMode + 292
12 HIToolbox 0x00007fff4270eee5 ReceiveNextEventCommon + 603
13 HIToolbox 0x00007fff4270ec76 _BlockUntilNextEventMatchingListInModeWithFilter + 64
14 AppKit 0x00007fff40aa779d _DPSNextEvent + 1135
15 AppKit 0x00007fff40aa648b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361
16 AppKit 0x00007fff40aa05a8 -[NSApplication run] + 699
17 AppKit 0x00007fff40a8fae8 NSApplicationMain + 777
18 background checker 0x0000000100000f55 main + 101
19 libdyld.dylib 0x00007fff6f4063d5 start + 1
20 ??? 0x0000000000000003 0x0 + 3
)

You’ve changed the method from checkProgress to checkProgress: with an argument, so you need to change the selector and object (if you use it) accordingly.

my performSelector:"checkProgress:" withObject:sender afterDelay:1.0

Thanks, that did it.
I also bought your book, hopefully will stop me having to ask for too much help.