有问题的后台任务可能会夺走焦点。它会打开一个窗口,该窗口会窃取焦点,并且很快就会关闭,但焦点不会返回。最近,Microsoft Office出现了一个错误。
要发现此类进程,可以使用诸如Window Focus Logger(镜像)之类的工具或类似于Process Monitor的自定义C#程序:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace ProcessMonitor
{
class Program
{
const int pollDelay = 100;
static void Main(string[] args)
{
var lastProcesses = GetDescriptions();
while (true)
{
System.Threading.Thread.Sleep(pollDelay);
var now = DateTime.Now;
var processes = GetDescriptions();
var started = processes.Except(lastProcesses);
var stopped = lastProcesses.Except(processes);
foreach (var p in started)
{
Print(now, p, ConsoleColor.Green);
}
foreach (var p in stopped)
{
Print(now, p, ConsoleColor.Red);
}
lastProcesses = processes;
}
}
static void Print(DateTime dateTime, ProcessDescription process,
ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine("{0:hh\\:mm\\:ss\\.ff}\tPID {1}\t{2}",
dateTime.TimeOfDay, process.Id, process.Description);
Console.ResetColor();
}
static List<ProcessDescription> GetDescriptions()
{
return Process.GetProcesses().Select(x => GetDescription(x)).ToList();
}
static ProcessDescription GetDescription(Process p)
{
int pid = -1;
string description;
try
{
pid = p.Id;
description = p.ProcessName;
}
catch (Exception e)
{
description = "Hit exception " + e;
}
return new ProcessDescription { Id = pid, Description = description };
}
struct ProcessDescription
{
public int Id;
public string Description;
public override bool Equals(object obj)
{
return obj != null && Id == ((ProcessDescription)obj).Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
}
}
Omar Alshaker提供的代码的经过抛光和错误修复的版本。也不需要C#6。需要.NET 3.5或更高版本。
您可以使用csc.exe
.NET Framework安装随附的C#编译器()对其进行编译,然后运行生成的可执行文件以获取开始(绿色)或结束(红色)进程的实时日志。使用Ctrl+ C终止它。
要查找编译器,请运行where /R %windir%\Microsoft.NET csc.exe
。无论是32b还是64b,都应从安装的最新.NET版本中选择一个。保存C#代码Program.cs
并编译为Program.exe
:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe Program.cs
msconfig
,然后选择启动选项卡。那里有什么节目?