Last active
August 15, 2021 00:50
-
-
Save gsuuon/dc24398339d1196a1e9d50d293727911 to your computer and use it in GitHub Desktop.
Monitor process / start stop - figure out what's stealing focus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Diagnostics | |
type KnownProcess = | |
{ pid : int | |
name : string | |
path: string | |
} | |
static member Show (x: KnownProcess) = | |
$"Pid ({x.pid})\t{x.name}\t| {x.path}" | |
let getProcessesSet () = | |
Process.GetProcesses() | |
|> Array.map | |
( fun p -> | |
{ pid = p.Id | |
name = p.ProcessName | |
path = | |
try | |
p.MainModule.FileName | |
with | |
| _ -> | |
"Couldn't get path" | |
} | |
) | |
|> Set | |
let rec monitorProcesses lastProcesses = | |
let thisProcesses = getProcessesSet() | |
let stoppedProcesses = lastProcesses - thisProcesses | |
let startedProcesses = thisProcesses - lastProcesses | |
if lastProcesses <> Set.empty then | |
stoppedProcesses | |
|> Seq.iter (KnownProcess.Show >> printfn "Stopped %s") | |
startedProcesses | |
|> Seq.iter (KnownProcess.Show >> printfn "Started %s") | |
System.Threading.Thread.Sleep 100 | |
monitorProcesses thisProcesses | |
[<EntryPoint>] | |
let main _argv = | |
printfn "Monitoring.." | |
monitorProcesses Set.empty | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Figure out what process is stealing focus
Steps:
Adapted (loosely) from answers here: https://superuser.com/questions/709052/active-window-program-unexpectedly-loses-focus-in-windows-7
In my case it was SearchApp.exe, which I guess is related to my having removed the annoying Cortana stuff :/