public void AsyncResultWaitHandle_AfterEndInvoke_IsSignalledOnSecondWait() { using (ActionThread thread = new ActionThread()) { thread.Start(); // Capture GenericSynchronizingObject ISynchronizeInvoke test = thread.DoGet(() => { return(new GenericSynchronizingObject()); }); IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { }), null); test.EndInvoke(result); result.AsyncWaitHandle.WaitOne(0); bool signalled = result.AsyncWaitHandle.WaitOne(0); Assert.IsTrue(signalled, "AsyncWaitHandle should be signalled"); } }
/// <summary> /// Helper method to raise an event on the correct thread /// </summary> private void RaiseEventOnUIThread(Delegate e, params object[] args) { if (e == null) { return; } foreach (Delegate d in e.GetInvocationList()) { ISynchronizeInvoke syncObj = d.Target as ISynchronizeInvoke; if (syncObj == null) { d.DynamicInvoke(args); } else { syncObj.EndInvoke(syncObj.BeginInvoke(d, args)); } } }
private void FireDataReceived(IExtendedLogOnInfo extendedLogOnInfo) { LogOnDeviceEventHandler eventHandler = DataReceived; if (eventHandler != null) { foreach (Delegate listener in eventHandler.GetInvocationList()) { ISynchronizeInvoke invoker = listener.Target as ISynchronizeInvoke; if (invoker != null && invoker.InvokeRequired) { // Marshal to the UI thread invoker.EndInvoke(invoker.BeginInvoke(listener, new object[] { extendedLogOnInfo })); } else { listener.DynamicInvoke(new object[] { extendedLogOnInfo }); } } } }
public static void InvokeLog(this ISynchronizeInvoke si, Delegate method, object[] args) { int pid = Thread.CurrentThread.ManagedThreadId; List <string> IST = null; bool listExists; lock (dicoLock) { listExists = InvokeStackTraces.TryGetValue(32, out IST); } if (listExists) { IST = new List <string>(); lock (dicoLock) { InvokeStackTraces.Add(pid, IST); } } IST.Add(Environment.StackTrace); var iar = si.BeginInvoke(method, args); si.EndInvoke(iar); lock (dicoLock) { InvokeStackTraces.Remove(InvokeStackTraces.Count - 1); if (InvokeStackTraces.Count == 0) { InvokeStackTraces.Remove(pid); } } }
/// <summary> /// Tries to safely invoke on the target thread the method represented by the current delegate. /// </summary> /// <param name="evt">The <see cref="Delegate"/> to invoke.</param> /// <param name="allow">A boolean to indicate whether to invoke the <see cref="Delegate"/> or not (for supporting CanRaiseEvents).</param> /// <param name="sender">The object raising the event.</param> /// <param name="e">The <see cref="EventArgs"/> to be passed on to the event handler.</param> /// <param name="synchronous">Whether the event should be raised synchronously or not.</param> public static void SafeRaise(this Delegate evt, bool allow, object sender, EventArgs e, bool synchronous) { if (evt == null || !allow) { return; } foreach (Delegate singleCast in evt.GetInvocationList()) { ISynchronizeInvoke syncInvoke = singleCast.Target as ISynchronizeInvoke; if (syncInvoke != null && syncInvoke.InvokeRequired) { var result = syncInvoke.BeginInvoke(singleCast, new object[] { sender, e }); if (synchronous) { var endResult = syncInvoke.EndInvoke(result); } } else { singleCast.DynamicInvoke(sender, e); } } }
object ISynchronizeInvoke.EndInvoke(IAsyncResult result) { return(_control.EndInvoke(result)); }
public object EndInvoke(IAsyncResult result) { return(SynchronizeInvoke.EndInvoke(result)); }
/// <summary> /// Special Invoke method that operates similarly to <see cref="ISynchronizeInvoke.Invoke"/> but in addition to that, it can be /// aborted by setting the stopEvent. This avoids potential deadlocks that are possible when using the regular /// <see cref="ISynchronizeInvoke.Invoke"/> method. /// </summary> /// <param name="method">A <see cref="System.Delegate"/> to a method that takes parameters of the same number and type that are /// contained in <paramref name="args"/></param> /// <param name="args">An array of type <see cref="System.Object"/> to pass as arguments to the given method. This can be null if /// no arguments are needed </param> /// <returns>The <see cref="Object"/> returned by the asynchronous operation</returns> private object StoppableInvoke(Delegate method, object[] args) { IAsyncResult asyncResult = invoker.BeginInvoke(method, args); waitHandles[0] = asyncResult.AsyncWaitHandle; return((WaitHandle.WaitAny(waitHandles) == 0) ? invoker.EndInvoke(asyncResult) : null); }
/// <summary> /// Waits until the process started by calling System.ComponentModel.ISynchronizeInvoke.BeginInvoke(System.Delegate,System.Object[]) /// completes, and then returns the value generated by the process. /// </summary> /// <param name="result">An System.IAsyncResult interface that represents the asynchronous operation /// started by calling System.ComponentModel.ISynchronizeInvoke.BeginInvoke(System.Delegate,System.Object[]).</param> /// <returns>An System.Object that represents the return value generated by the asynchronous operation.</returns> public object EndInvoke(IAsyncResult result) { return(_sync.EndInvoke(result)); }
object ISynchronizeInvoke.EndInvoke(IAsyncResult result) { // throw new NotImplementedException(); return(m_Synchronizer.EndInvoke(result)); }