等待任务时忽略用户输入 - Objective-C

发布于 2024-08-13 09:40:56 字数 295 浏览 15 评论 0原文

我有一个带有“转换”按钮的应用程序。当我单击此按钮时,应用程序使用 NSTask 启动一个新进程,然后使该按钮变灰,直到该进程完成。

我的问题是,应用程序会保存用户在等待过程完成时所做的任何点击。因此,即使按钮呈灰色,用户也可以单击它,并且该过程一旦完成就会立即重新开始。

我正在等待进程完成,使用:

[task waitUntilExit];

How do Iignore any user input while waiting this task to finish?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-08-20 09:40:56

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

我会做这样的事情,而不是 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];
}

这将启动您的任务,禁用按钮,但不会阻止线程,因为您没有等待它完成。相反,您正在等待任务完成时的异步通知。由于该按钮在任务完成之前处于禁用状态,因此这将忽略发送给它的所有事件。

-[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.

Instead of waitUntilExit, I'd do something like this:

- (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.

我的奇迹 2024-08-20 09:40:56

甚至更容易:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

even easier:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文