(以下假设“运行”是指在同一进程中长时间运行的操作,并且不使用 wxExecute 或 wxProcess 运行外部进程。)
“取消”事件没有被触发,因为通过执行你的运行逻辑你没有给 UI 处理点击事件的机会。
为避免阻塞 UI,您需要执行以下操作。当您单击运行按钮时,围绕您要运行的函数创建一个协程:
coro = coroutine.create(myLongRunningFunction)
此时您的跑步活动已完成。然后在 EVT_IDLE 事件中,只要它不完整,你就会恢复这个协程。它看起来像这样:
if coro then -- only if there is a coroutine to work on
local ok, res = coroutine.resume(coro, additional, parameters)
-- your function either yielded or returned
-- you may check ok to see if there was an error
-- res can tell you how far you are in the process
-- coro can return multiple values (just give them as parameters to yield)
if coroutine.status(coro) == 'dead' then -- finished or stopped with error
coro = nil
-- do whatever you need to do knowing the process is completed
end
end
只要您的进程未完成,您可能需要请求更多 IDLE 事件,因为某些操作系统不会触发 IDLE 事件,除非触发了其他一些事件。假设您的处理程序具有event 参数,您可以执行event:RequestMore(true) 来请求更多IDLE 事件(RequestMore)。
您的长时间运行的进程需要在正确的时间调用 coroutine.yield() (不要太短,因为您会浪费时间来回切换,也不要太长时间让用户注意到 UI 中的延迟);您可能需要对此进行试验,但调用之间 100 毫秒左右的基于计时器的方法可能会起作用。
您可以像现在一样在 IDLE 事件处理程序或长时间运行的函数中检查 Cancel 值。我描述的逻辑将使您的应用程序 UI 有机会按您的预期处理 Cancel 事件。