/// <summary> /// Executes an <code>Action</code> with a time limit in an separate asynchronous Thread and waits until it ends for the return. /// </summary> /// <param name="maxTime">The maximum execution time in milliseconds. After this time the thread will be abort!</param> /// <param name="codeBlock">The code block. Us a e.g. an anonymous delegate such as /// <code>() => { /* Write your time bounded code here*/ }</code></param> /// <param name="name">The name.</param> /// <returns>indication if the execution was done successfully</returns> public static bool WaitForExecuteWithTimeLimit(int maxTime, Action codeBlock, string name = "") { bool success = false; try { var executor = TimeLimitExecutor.ExecuteWithTimeLimit(maxTime, codeBlock, name); while (executor != null && executor.IsAlive && executor.ThreadState == System.Threading.ThreadState.Running) { Thread.Sleep(1); } success = true; } catch (System.Threading.ThreadAbortException) { return(false); } return(success); }
/// <summary> /// Calls an actions with a previously selection change and a selection reset afterwards. /// </summary> /// <param name="act">The action to call.</param> /// <param name="selectProv">The selection provider - commonly this is the document.</param> /// <param name="selection">The selection. Use <c>null</c> to reset the selection.</param> internal static void ActionWithChangeAndResetSelection(Action act, XSelectionSupplier selectProv, Object selection = null) { try { TimeLimitExecutor.ExecuteWithTimeLimit(1000, () => { try { var oldSel = GetSelection(selectProv); Thread.Sleep(80); var succ = SetSelection(selectProv, selection); Thread.Sleep(80); if (succ && act != null) { act.Invoke(); if (Thread.CurrentThread.IsAlive) { Thread.Sleep(250); } } Thread.Sleep(100); SetSelection(selectProv, oldSel); } catch (Exception ex) { Logger.Instance.Log(LogPriority.ALWAYS, "OoDispatchHelper", "[FATAL ERROR] Can't call dispatch command with selection - Thread interrupted:", ex); } }, "Delete Object"); } catch (ThreadInterruptedException ex) { Logger.Instance.Log(LogPriority.ALWAYS, "OoDispatchHelper", "[FATAL ERROR] Can't call dispatch command with selection - Thread interrupted:", ex); } catch (Exception ex) { Logger.Instance.Log(LogPriority.ALWAYS, "OoDispatchHelper", "[FATAL ERROR] Can't call dispatch command with selection:", ex); } }