/// <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)
        {
            try
            {
                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();
                        _currentInstance = null;
                        return null;
                    }
                    else
                    {
                        _currentInstance = null;
                    }

                }
                return Result.ToArray();
            }
            catch (Exception exception)
            {
                DebugConsole.Default.WriteException(exception);
                throw;
            }
        }
示例#2
0
 /// <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)
 {
     try
     {
         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();
                 _currentInstance = null;
                 return(null);
             }
             else
             {
                 _currentInstance = null;
             }
         }
         return(Result.ToArray());
     }
     catch (Exception exception)
     {
         DebugConsole.Default.WriteException(exception);
         throw;
     }
 }
        private static List<object> GetActiveExcelApplicationProxiesFromROT()
        {
            try
            {
                WindowEnumerator enumerator = new WindowEnumerator("XLMAIN");
                IntPtr[] handles = enumerator.EnumerateWindows(2000);
                if (null == handles || handles.Length == 0)
                    return new List<object>();

                return ExcelApplicationWindow.GetApplicationProxiesFromHandle(handles);
            }
            catch (Exception exception)
            {
                DebugConsole.Default.WriteException(exception);
                throw;
            }
        }