【发布时间】:2024-07-27 05:15:02
【问题描述】:
我正在使用 getRunningPrcoess() 函数获取所有正在运行的进程。但是当我试图终止某个特定进程时,终止功能不起作用,它不会终止该进程。请告诉我我的代码有什么问题。我正在使用 killProcess(_ processId: Int) 函数并在参数中传递进程 ID 来终止进程。有没有其他方法可以从您的应用程序中终止正在运行的进程?
//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
}
【问题讨论】:
标签: swift macos process kill terminate