static void Main(string[] args) { WindowEnumerator enumerator = new WindowEnumerator("XLMAIN"); DateTime startTime = DateTime.Now; IntPtr[] handles = enumerator.EnumerateWindows(1000); if (null != handles) { foreach (IntPtr item in handles) { object proxy = ExcelApplicationWindow.GetApplicationProxyFromHandle(item); if (null != proxy) { NetOffice.ExcelApi.Application application = new NetOffice.ExcelApi.Application(null, proxy); Console.WriteLine("Excel.Application Hwnd:{0}", application.Hwnd); } } } else { Console.WriteLine("Enumerate Windows failed because the timeout is reached."); } Console.WriteLine("Press any key.."); Console.ReadKey(); }
/// <summary> /// Enumerates all top level windows on desktop. WARNING: The method returns null if operation timeout is reached. /// </summary> /// <param name="milliSecondsTimeout">a timeout for the operation. when a desktop is busy or non responding these method freeze. you can handle this with the operation timeout</param> /// <returns>Result Array or null</returns> public IntPtr[] EnumerateWindows(int milliSecondsTimeout) { lock (_lockInstance) { Result.Clear(); _currentInstance = this; Thread thread1 = new Thread(new ParameterizedThreadStart(EnumerateWindowsAsync)); WaitHandle[] waitHandles = new WaitHandle[1]; ManualResetEvent mre1 = new ManualResetEvent(false); waitHandles[0] = mre1; thread1.Start(mre1); bool result = WaitHandle.WaitAll(waitHandles, milliSecondsTimeout); if (!result) { thread1.Abort(); Result.Clear(); return null; } } return Result.ToArray(); }
/// <summary> /// Enumerates all top level windows on desktop. WARNING: The method returns null if operation timeout is reached. /// </summary> /// <param name="milliSecondsTimeout">a timeout for the operation. when a desktop is busy or non responding these method freeze. you can handle this with the operation timeout</param> /// <returns>Result Array or null</returns> public IntPtr[] EnumerateWindows(int milliSecondsTimeout) { lock (_lockInstance) { Result.Clear(); _currentInstance = this; Thread thread1 = new Thread(new ParameterizedThreadStart(EnumerateWindowsAsync)); WaitHandle[] waitHandles = new WaitHandle[1]; ManualResetEvent mre1 = new ManualResetEvent(false); waitHandles[0] = mre1; thread1.Start(mre1); bool result = WaitHandle.WaitAll(waitHandles, milliSecondsTimeout); if (!result) { thread1.Abort(); Result.Clear(); return(null); } } return(Result.ToArray()); }