コンピューターの使用状況を監視するプログラムを作成しています。このプログラムの一部では、前景ウィンドウが何であるかを常に知っている必要があります。これは、次のコードによって実現されます。
// Returns the name of the process owning the foreground window.
private static Process GetForegroundProcess()
{
try
{
IntPtr hwnd = GetForegroundWindow();
// The foreground window can be NULL in certain circumstances,
// such as when a window is losing activation.
uint pid;
GetWindowThreadProcessId(hwnd, out pid);
Process p = Process.GetProcessById((int)pid);
return p;
}
catch (Exception)
{
return null;
}
}
私が抱えている問題はCPU消費です。これは、毎秒ティックするティッカー内で呼び出されるため、徐々にすべての CPU を使用します。プログラムのコア機能には、毎秒カチカチ音をたてることが必要です。
私の質問は、私のプログラムがコンピューターをフリーズさせずにこれを行う方法はありますか?
お時間とご回答ありがとうございます。