public static Win GetnewWindow(this Win win, string cls, string name = "") { Stopwatch sw = new Stopwatch(); sw.Start(); List <int> oldPtrs = win.allWindowPtrs; do { win.GetAllWindows(); var diffWindows = win.allWindowPtrs.Where(x => oldPtrs.Contains(x) == false).Select(x => new Win { Handle = x }); Console.WriteLine(string.Format("different windows count:{0}", diffWindows.Count())); diffWindows.ToList().ForEach(x => { Console.WriteLine(string.Format("Handle:{0} cls:{1} name:{2}", x.Handle, x.Class, x.Name)); }); if (string.IsNullOrEmpty(cls) == false) { diffWindows = diffWindows.Where(x => x.Class.Contains(cls) && x.Name.Contains(name)); } if (diffWindows.Count() > 0) { return(diffWindows.First()); } Thread.Sleep(200); }while (sw.ElapsedMilliseconds < timeOut); return(null); }