Cocoa终止一个进程 [英] Terminate a process by Cocoa

查看:419
本文介绍了Cocoa终止一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我终止自己,只需使用 [NSApp terminate:nil] ,它工作得很好。但是,如果我想终止另一个进程,如活动监视器,我应该怎么办?

If I terminate myself, just use the [NSApp terminate: nil], it works very well. But if I want to terminate another process such as the active monitor, what should I do?

要获得进程列表,我使用

To get the list of processes, I use

NSArray* processlist = [[NSWorkspace sharedWorkspace] runningApplications];

我是对吗?但是我如何可以通过Cocoa终止一个进程,而不是使用 kill KillProcess(< #const ProcessSerialNumber * inProcess#>) killpd 或类似的东西,我刚开始学习Cocoa,所以也许我需要一些简单的示例代码或一些关键字,可以帮助我找到文档。感谢您的帮助。

Am I right? But how I can terminate a process by Cocoa, not use the kill or KillProcess(<#const ProcessSerialNumber *inProcess#>) or killpd or something like those, I just start learning Cocoa, so maybe I need some simple sample code or some keywords that can help me to find the documents. Thank you for your help.

推荐答案

- [NSWorkspace runningApplications] 一个 NSRunningApplication 对象的数组。 NSRunningApplication 有一个方法 - [NSRunningApplication terminate] 。所以如果你正在寻找一个特定的应用程序,你可以像这样终止它:

-[NSWorkspace runningApplications] returns an array of NSRunningApplication objects. NSRunningApplication has a method -[NSRunningApplication terminate]. So if you are looking for a specific application, you could terminate it like this:

-(void)killProcessesNamed:(NSString*)appName
{
    for ( id app in [[NSWorkspace sharedWorkspace] runningApplications] ) 
    {
        if ( [appName isEqualToString:[[app executableURL] lastPathComponent]] ) 
        {
            [app terminate];
        }
    }
}

c $ c> forceTerminate 强制应用退出,而不执行正常的退出过程。 (它不会要求保存更改等)

You could also call forceTerminate to force the app to quit without the normal quitting process. (It won't ask to save changes, etc.)

还有其他方法 NSRunningApplication 使用这个过程更简单,取决于您是否正在搜索基于bundle ID或PID的进程。

There are other methods of NSRunningApplication that you can use to make this process simpler depending on whether or not you're searching for a process based on bundle ID or PID.

这篇关于Cocoa终止一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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