簡體   English   中英

進程沒有終止 macOS 應用程序/終止正在運行的進程

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

我正在使用getRunningPrcoess() function 獲取所有運行過程。 但是當我試圖終止某些特定進程時,終止 function 不起作用,它不會終止進程。 請告訴我我的代碼有什么問題。 我正在使用killProcess(_ processId: Int) function 並在參數中傳遞進程 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
}

我認為您正在初始化一個新進程而不是終止現有進程。 .init()創建一個具有相同標識符的新進程,然后將其殺死。 所以你需要在這里改變你的方法。

更新:正確的解決方案是在項目設置的功能部分中關閉沙盒模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM