简体   繁体   English

进程没有终止 macOS 应用程序/终止正在运行的进程

[英]Process isn't terminating macOS Application / Killing Running Process

I am getting all the running process using getRunningPrcoess() function.我正在使用getRunningPrcoess() function 获取所有运行过程。 But when I tried to terminate some specific process then the termination function is not working, it doesn't kill the process.但是当我试图终止某些特定进程时,终止 function 不起作用,它不会终止进程。 Please tell me What's wrong with my code.请告诉我我的代码有什么问题。 I am using killProcess(_ processId: Int) function and passing the process ID in the parameter to terminate the process.我正在使用killProcess(_ processId: Int) function 并在参数中传递进程 ID 来终止进程。 Is there any other way to kill the running process from your application?有没有其他方法可以从您的应用程序中终止正在运行的进程?

//MARK:- Variables
var arrApplication: [NSRunningApplication]!

//MARK:- Load
override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
    tblRunningProcess.delegate = self
    tblRunningProcess.dataSource = self
    getRunningPrcoess()
}

//MARK:- Actions
@IBAction func btnEndTaskAction(_ sender: NSButton) {
    arrApplication[sender.tag].forceTerminate()
    getRunningPrcoess()
}

//MARK:- Functions
func getRunningPrcoess() {
    // Get all running applications
    let workspace = NSWorkspace.shared
    arrApplication = workspace.runningApplications
    tblRunningProcess.reloadData()
}

func numberOfRows(in tableView: NSTableView) -> Int {
    return arrApplication == nil ? 0 : arrApplication.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    guard let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "RunningProcessTCell"), owner: self) as? RunningProcessTCell else { return nil }
    cell.lblProcessName.stringValue = "Name: \(arrApplication[row].localizedName ?? "N/A")"
    cell.lblProcessId.stringValue = "ID: \(arrApplication[row].bundleIdentifier ?? "N/A")"
    cell.imgIcon.image = arrApplication[row].icon
    cell.btnEndTask.tag = row
    return cell
}

I think you are initializing a new process instead of killing the existing one.我认为您正在初始化一个新进程而不是终止现有进程。 .init() creates a new process with same identifier and then it's killed. .init()创建一个具有相同标识符的新进程,然后将其杀死。 So you need to change your approach here.所以你需要在这里改变你的方法。

Update: Right solution is to turn off sandbox mode in the Capabilities section of the Project Settings.更新:正确的解决方案是在项目设置的功能部分中关闭沙盒模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM