I’m using some code from MBS that is run 3 times a second by a timer from a menubar app.
Activity Monitor tells me my app is using 96% CPU. It doesn’t seem to slow anything down, but it shouldn’t be this high.
The list returned by NSRunningApplicationMBS.runningApplications includes tons of background processes.
I have 7 apps open, but the list it returns has 108 items, and it has to check each one to see if that is the active (frontmost) one.
is there a better, less intensive way to get the name of the active or frontmost app?
Var n(-1) As NSRunningApplicationMBS = NSRunningApplicationMBS.runningApplications
Var t As String
For Each r As NSRunningApplicationMBS In n
If r.Active Then
t = r.localizedName
Exit
End If
Next
That sounds odd - please click the “Sample” button in Activity Monitor and post the results here, so we can see what is using all the CPU - it could be something unexpected.
Since you have MBS, you could do this:
Public Function GetFrontProcessInfo() as String
dim p as new ProcessMBS
p.GetFrontProcess
return p.name
End Function
Edit to add: Another advantage of this method is it works on mac and windows.
@Mark_Sweeney I just ran a test with your code, and the app is using about 1% CPU. Are you sure your timer is running 3 times per second? (333 millisecond period)
It turns out the MBS code I was using was not the culprit. I thought getting the list and comparing it multiple times a second might have been spiking something.
It turns out I still need a part of that code to see if another menubar app is running.
FWIW I have written a Tracking App, but unfortunately this MBS function doesn’t inform you what documents are open within the running apps, plus it is macOS only. For this reason I use a Shell command.