private void pollingTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            ActiveApplication currentActiveWindow = GetActiveWindow();

            OnChanged(currentActiveWindow);
        }
        public ActiveApplication GetActiveWindow()
        {
            ActiveApplication activeApplication = null;

            try
            {
                var handle = WindowsFunctions.GetForegroundWindow();
                int processID;

                WindowsFunctions.GetWindowThreadProcessId(handle, out processID);

                Process proc = Process.GetProcessById(processID);

                const int     nChars = 512;
                StringBuilder Buff   = new StringBuilder(nChars);
                string        title  = "";

                if (WindowsFunctions.GetWindowText(handle, Buff, nChars) > 0)
                {
                    title = Buff.ToString();
                }
                else
                {
                    try
                    {
                        title = proc.MainWindowTitle;
                    }
                    catch (Exception ex)
                    {
                        OnError(ex);
                    }
                }

                string processName = proc.ProcessName;

                DocumentInfo documentInfo = GetDocument(new ApplicationInfo(
                                                            processID,
                                                            proc.ProcessName,
                                                            null,
                                                            null,
                                                            null,
                                                            null,
                                                            null,
                                                            null,
                                                            null,
                                                            handle,
                                                            title
                                                            ));

                activeApplication = new ActiveApplication
                {
                    Title        = title,
                    ProcessName  = processName ?? "",
                    ProcessId    = processID,
                    DocumentInfo = documentInfo
                };
            }
            catch (Exception ex)
            {
                OnError(ex);
            }

            return(activeApplication);
        }
 protected void OnChanged(ActiveApplication activeApplication)
 {
     Changed(this, new EventArgs <ActiveApplication>(activeApplication));
 }