Skip to content

Instantly share code, notes, and snippets.

@gsuuon
Last active August 15, 2021 00:50
Show Gist options
  • Save gsuuon/dc24398339d1196a1e9d50d293727911 to your computer and use it in GitHub Desktop.
Save gsuuon/dc24398339d1196a1e9d50d293727911 to your computer and use it in GitHub Desktop.
Monitor process / start stop - figure out what's stealing focus
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
@gsuuon
Copy link
Author

gsuuon commented Aug 15, 2021

Figure out what process is stealing focus

Steps:

  1. Run this
  2. Alt-f4 when focus gets stolen
  3. Check console to see which processes stopped
  4. Do it a few times to be sure

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 :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment