Exemplo n.º 1
0
 private void UpdateWindowList()
 {
     try
     {
         ToggleLoading(LoadingType.Start);
         var stopWatch = new Stopwatch();
         stopWatch.Start();
         var tempWindowList      = new List <WindowListItem>();
         BackgroundWorker worker = new BackgroundWorker()
         {
             WorkerReportsProgress = true
         };
         worker.DoWork += (ws, we) =>
         {
             try
             {
                 foreach (var proc in Process.GetProcesses())
                 {
                     try
                     {
                         var rect = WindowInfo.GetProcessDimensions(proc);
                         uDebugLogAdd($"Process {proc.ProcessName} | T{rect.Top} L{rect.Left} H{rect.Bottom - rect.Top} W{rect.Right - rect.Left}");
                         if (WindowInfo.DoesProcessHandleHaveSize(proc))
                         {
                             uDebugLogAdd($"Process {proc.ProcessName} has size");
                             if (tempWindowList.Find(x => x.Process.MainWindowHandle == proc.MainWindowHandle) == null)
                             {
                                 uDebugLogAdd($"Process {proc.ProcessName} doesn't currently exist in the windowList, adding process");
                                 foreach (var handle in WinAPIWrapper.EnumerateProcessWindowHandles(proc.Id))
                                 {
                                     try
                                     {
                                         if (WindowInfo.DoesHandleHaveSize(handle) && WinAPIWrapper.IsWindowVisible(handle))
                                         {
                                             var windowListItem = WindowListItem.Create(proc, handle);
                                             if (tempWindowList.Find(x => x.Display == windowListItem.Display) == null)
                                             {
                                                 tempWindowList.Add(windowListItem);
                                                 uDebugLogAdd($"Added to list | [{windowListItem.Handle}]{windowListItem.Display}");
                                             }
                                             else
                                             {
                                                 uDebugLogAdd($"Item already in the list, skipping | {windowListItem.Display}");
                                             }
                                         }
                                         else
                                         {
                                             uDebugLogAdd($"Handle window doesn't have size, skipping | [{handle}]{proc.ProcessName}");
                                         }
                                     }
                                     catch (Exception ex)
                                     {
                                         uDebugLogAdd($"Unable to add handle to the list | [{handle}]{proc.ProcessName}: {ex.Message}");
                                     }
                                 }
                             }
                             else
                             {
                                 uDebugLogAdd($"Already enumerated through handles for {proc.ProcessName}, skipping this one");
                             }
                         }
                     }
                     catch (Exception ex)
                     {
                         uDebugLogAdd($"Unable to get proc {proc.ProcessName}: {ex.Message}");
                     }
                 }
             }
             catch (Exception ex)
             {
                 LogException(ex);
             }
             worker.ReportProgress(1);
         };
         worker.ProgressChanged += (ps, pe) =>
         {
             if (pe.ProgressPercentage == 1)
             {
                 lbProcList.ItemsSource = null;
                 WindowList             = tempWindowList.OrderBy(x => x.Display).ToList();
                 lbProcList.ItemsSource = WindowList;
                 stopWatch.Stop();
                 uDebugLogAdd($"Updated window list, took: {stopWatch.Elapsed.Seconds}s {stopWatch.Elapsed.Milliseconds}ms");
                 ToggleLoading(LoadingType.Done);
             }
         };
         worker.RunWorkerAsync();
     }
     catch (Exception ex)
     {
         LogException(ex);
     }
 }