How to use NSAutoreleasePool in AppleScriptObjC

旧城冷巷雨未停 提交于 2019-12-20 04:37:18

问题


I am wondering how to stop another function from a background function.
In addition, I have to drain NSAutoreleasePool, but I don't know how to do it.
I think this app sometimes freeze if I don't release pool.

property i : 0
property myLabel : missing value

on myStartButtonHandler_(sender)
    my performSelectorInBackground_withObject_("start", missing value) -- This code prevents "Stop" Button from freezing.
end myStartButtonHandler_

on myStopButtonHandler_(sender)
    -- I want to stop start() function and drain AutoreleasePool!
    -- I don't want to use "quit me" because I want to stop only start() function.
end myStopButtonHandler_

on start()
    repeat
        set i to i + 1
        myLabel's setIntegerValue_(i)
        delay 1
    end repeat
end start

You can download source code from here --> https://dl.dropboxusercontent.com/u/97497395/test2.zip
For your information, I am using Xcode 4.6.3.

EDIT
My script has delay 300 command, so I can't stop the function with checking the value of the variable. Sorry.

EDIT
I conceived of an idea to stop the function while delay commands.

on start()
    repeat 5 times
        if i = 1 then
            error -128
        else
            delay 60
    end repeat
end start
on myStopButtonHandler_(sender)
    set i to 1
end myStopButtonHandler_

I can stop the function in 60 seconds, but I can't stop it as soon as I push the stop button. So, I am still waiting for your answer.


回答1:


An easy way to stop the function is to have a variable. Check the value of the variable. If the variable is true for example then you can exit your repeat loop and drain the autorelease pool. I'm running to work now so no time to write code. Good luck.

EDIT: If you use an NSTimer to fire your handler, as opposed to a repeat loop, then you can invalidate the timer to stop it from running the handler. I use this to invalidate a timer because you should always check that the timer is valid before invalidating it... it will crash if you invalidate a non-valid timer.

-(void)deleteMyTimer {
    if ([myTimer isValid]) {
        [myTimer invalidate]
        myTimer = nil;
    }
}


来源:https://stackoverflow.com/questions/18199982/how-to-use-nsautoreleasepool-in-applescriptobjc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!