如何在AppleScriptObjC中使用NSAutoreleasePool [英] How to use NSAutoreleasePool in AppleScriptObjC
问题描述
我想知道如何从后台函数中停止另一个函数。
另外,我必须消耗NSAutoreleasePool,但我不知道该怎么做。
我认为这个应用有时会冻结,如果我不释放池的话。
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
您可以从此处下载源代码-> < a href = https://dl.dropboxusercontent.com/u/97497395/test2.zip rel = nofollow> https://dl.dropboxusercontent.com/u/97497395/test2.zip
供您参考,我正在使用Xcode 4.6.3。
编辑
我的脚本中有 delay 300
命令,因此无法通过检查变量的值来停止该函数。抱歉。
编辑
我想到了一个在延迟命令时停止功能的想法。
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_
我可以在60秒内停止该功能,但是一旦按下停止按钮就无法停止该功能。因此,我仍在等待您的答案。
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.
推荐答案
停止函数的一种简单方法是拥有一个变量。检查变量的值。例如,如果变量为true,则可以退出重复循环并耗尽自动释放池。我现在要去上班,所以没有时间写代码。
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 :如果您使用NSTimer来触发处理程序,而不是重复循环,则可以使计时器无效以使其停止运行处理程序。我用它来使计时器无效,因为您应该在使计时器无效之前始终检查计时器是否有效...如果使无效计时器无效,它将崩溃。
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;
}
}
这篇关于如何在AppleScriptObjC中使用NSAutoreleasePool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!