在等待任务时忽略用户输入 - Objective-C [英] Ignoring user input while waiting for a task - Objective-C

查看:162
本文介绍了在等待任务时忽略用户输入 - Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有转换按钮的应用。当我点击这个按钮,应用程序使用NSTask启动一个新的过程,然后灰色的按钮,直到过程完成。

I have an app with a button 'convert'. When I click this button, the app starts a new process using NSTask and then greys out the button until the process finishes.

我的问题是,应用程序保存任何点击由用户在等待过程完成时。因此,即使按钮变灰,用户也可以点击它,流程一旦完成就立即重新开始。

My problem is, the app saves any clicks made by the user while it is waiting for the process to finish. So even though the button is grayed out, the user can click it and the process will immediately start again as soon as it finishes.

我在等待过程完成使用:

I'm waiting for the process to finish using:

[task waitUntilExit];

如何在等待此任务完成时忽略任何用户输入?

How do I ignore any user input while waiting for this task to finish?

推荐答案

- [NSTask waitUntilExit] 当然是一个阻塞调用。这意味着线程暂停(和运行循环一样),并且发送到线程的所有事件都会排队等待,直到运行循环可以处理它们。

-[NSTask waitUntilExit] is, of course, a blocking call. That means the thread pauses (as does the run loop) and all the events that are sent to the thread are queued up until the run loop can process them.

waitUntilExit ,我会这样做:

- (IBAction) myButtonMethod {
  NSTask * task = ....;
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskFinished:) name:NSTaskDidTerminateNotification object:task];
  [myButton setEnabled:NO];
  [task launch];
}

- (void) taskFinished:(NSNotification *)note {
  [myButton setEnabled:YES];
}



这将启动你的任务,禁用按钮,因为你不等待它完成。相反,您正在等待任务完成时的异步通知。由于按钮在任务完成之前被禁用,因此将忽略发送给它的所有事件。

This will launch your task, disable the button, but not block the thread, because you're not waiting for it to finish. Instead, you're waiting for the asynchronous notification of whenever the task finishes. Since the button is disabled until the task finishes, this will ignore all events sent to it.

这篇关于在等待任务时忽略用户输入 - Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆