I am getting all the running process using getRunningPrcoess() function. But when I tried to terminate some specific process then the termination function is not working, it doesn't kill the process. 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. 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. 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.