private void PrepareCaptureMenuAsync(ToolStripMenuItem tsmiWindow, EventHandler handlerWindow, ToolStripMenuItem tsmiMonitor, EventHandler handlerMonitor) { tsmiWindow.DropDownItems.Clear(); WindowsList windowsList = new WindowsList(); List<WindowInfo> windows = null; TaskEx.Run(() => { windows = windowsList.GetVisibleWindowsList(); }, () => { if (windows != null) { foreach (WindowInfo window in windows) { try { string title = window.Text.Truncate(50); ToolStripItem tsi = tsmiWindow.DropDownItems.Add(title); tsi.Tag = window; tsi.Click += handlerWindow; using (Icon icon = window.Icon) { if (icon != null && icon.Width > 0 && icon.Height > 0) { tsi.Image = icon.ToBitmap(); } } } catch (Exception e) { DebugHelper.WriteException(e); } } } tsmiMonitor.DropDownItems.Clear(); Screen[] screens = Screen.AllScreens; for (int i = 0; i < screens.Length; i++) { Screen screen = screens[i]; string text = string.Format("{0}. {1}x{2}", i + 1, screen.Bounds.Width, screen.Bounds.Height); ToolStripItem tsi = tsmiMonitor.DropDownItems.Add(text); tsi.Tag = screen.Bounds; tsi.Click += handlerMonitor; } tsmiWindow.Invalidate(); tsmiMonitor.Invalidate(); }); }