namespace WindowMonitor { using System; using System.ServiceProcess; using System.Timers; /// /// Background service to monitor window status. /// public partial class WindowMonitorService : ServiceBase { /// /// Timer to use to schedule window checks. /// private Timer timer; /// /// The window monitor to use. /// private WindowMonitor monitor; /// /// Initializes a new instance of the class. /// public WindowMonitorService() { this.InitializeComponent(); } /// /// Starts the service. /// public void Startup() { this.monitor = new WindowMonitor(); this.timer = new Timer(1000); this.timer.Elapsed += this.OnTimerElapsed; this.timer.Start(); } /// /// Stops the service. /// public void Shutdown() { this.timer.Stop(); this.timer.Elapsed -= this.OnTimerElapsed; this.timer.Dispose(); this.timer = null; this.monitor = null; } /// /// Called when the service is being started by the Windows host. /// /// /// The arguments given to the service. /// protected override void OnStart(string[] args) { this.Startup(); } /// /// Called when the service is being stopped by the Windows host. /// protected override void OnStop() { this.Shutdown(); } /// /// Called when our timer elapses, triggering us to do some work. /// /// /// The timer that sent the event. /// /// /// The event arguments. /// private void OnTimerElapsed(object sender, ElapsedEventArgs e) { var handle = this.monitor.GetActiveWindowHandle(); Console.WriteLine(this.monitor.GetWindowTitle(handle)); Console.WriteLine(this.monitor.GetWindowFileName(handle)); Console.WriteLine(); } } }