public AbstractFileFactory(string fileExtension) { FFileExtension = new List <string>(); FFileExtension.AddRange(fileExtension.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); Debug.Assert(SynchronizationContext.Current != null, "SynchronizationContext not set."); FSyncContext = new GenericSynchronizingObject(); }
public void UnknownGSO_NeverRequiresInvoke() { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new MySynchronizationContext())) { GenericSynchronizingObject test = new GenericSynchronizingObject(); Assert.IsFalse(test.InvokeRequired, "Unknown GenericSynchronizingObject does require invoke"); } }
public void FailingAction_InvokedThroughThreadPoolGSO_ThrowsTargetInvocationException() { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext())) { GenericSynchronizingObject test = new GenericSynchronizingObject(); test.Invoke((MethodInvoker)(() => { throw new MyException(); }), null); } }
public void GSO_FromThreadPool_DoesNotRequireInvoke() { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext())) { GenericSynchronizingObject test = new GenericSynchronizingObject(); Assert.IsFalse(test.InvokeRequired, "GenericSynchronizingObject does require invoke within thread pool"); } }
public void ThreadPoolGSO_FromChildThread_DoesRequireInvoke() { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext())) using (ActionThread thread = new ActionThread()) { GenericSynchronizingObject test = new GenericSynchronizingObject(); thread.Start(); bool invokeRequired = thread.DoGet(() => test.InvokeRequired); Assert.IsTrue(invokeRequired, "ThreadPool GenericSynchronizingObject does not require invoke from within a child thread"); } }
public void ActionThreadGSO_OutsideActionThread_DoesRequireInvoke() { using (ActionThread thread = new ActionThread()) { thread.Start(); // Capture GenericSynchronizingObject GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); }); Assert.IsTrue(test.InvokeRequired, "GenericSynchronizingObject does not require invoke for ActionThread"); } }
public void ThreadPoolGSOFromNullSyncContext_Invoked_ExecutesSynchronously() { int threadId = ~Thread.CurrentThread.ManagedThreadId; using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(null)) { GenericSynchronizingObject test = new GenericSynchronizingObject(); test.Invoke((MethodInvoker)(() => { threadId = Thread.CurrentThread.ManagedThreadId; }), null); } Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, threadId, "ThreadPool invoke did not operate synchronously"); }
public void FailingAction_InvokedThroughActionThreadGSO_ThrowsTargetInvocationException() { using (ActionThread thread = new ActionThread()) { thread.Start(); // Capture GenericSynchronizingObject GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); }); test.Invoke((MethodInvoker)(() => { throw new Exception(); }), null); } }
public void Action_InvokedThroughActionThreadGSO_RunsOnTheActionThread() { using (ActionThread thread = new ActionThread()) { thread.Start(); // Capture GenericSynchronizingObject GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); }); int actionThreadId = Thread.CurrentThread.ManagedThreadId; test.Invoke((MethodInvoker)(() => { actionThreadId = Thread.CurrentThread.ManagedThreadId; }), null); Assert.AreEqual(thread.ManagedThreadId, actionThreadId, "GenericSynchronizingObject.Invoke did not synchronize"); } }
public void Action_InvokedThroughActionThreadGSO_Runs() { bool sawAction = false; using (ActionThread thread = new ActionThread()) { thread.Start(); // Capture GenericSynchronizingObject GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); }); test.Invoke((MethodInvoker)(() => { sawAction = true; }), null); Assert.AreEqual(true, sawAction, "GenericSynchronizingObject.Invoke did not execute action"); } }
public void ThreadPoolGSO_FromThreadPool_DoesNotRequireInvoke() { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext())) { GenericSynchronizingObject test = new GenericSynchronizingObject(); bool invokeRequired = true; using (ManualResetEvent evt = new ManualResetEvent(false)) { ThreadPool.QueueUserWorkItem(_ => { invokeRequired = test.InvokeRequired; evt.Set(); }, null); evt.WaitOne(); } Assert.IsFalse(invokeRequired, "ThreadPool GenericSynchronizingObject does require invoke from within thread pool"); } }
public void Action_BeginInvokeThroughActionThreadGSO_Runs() { bool sawAction = false; using (ActionThread thread = new ActionThread()) { thread.Start(); // Capture GenericSynchronizingObject GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); }); IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null); test.EndInvoke(result); Assert.IsTrue(sawAction, "GenericSynchronizingObject.BeginInvoke did not execute action"); } }
public void Action_InvokedThroughActionThreadGSO_ReceivesParameters() { object parameter = new object(); object argument = null; using (ActionThread thread = new ActionThread()) { thread.Start(); // Capture GenericSynchronizingObject GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); }); test.Invoke((Action <object>)((arg) => { argument = arg; }), new [] { parameter }); Assert.AreSame(parameter, argument, "GenericSynchronizingObject.Invoke did not pass parameter"); } }
public void Action_BeginInvokeThroughThreadPoolGSO_RunsOnAThreadPoolThread() { using (ActionThread thread = new ActionThread()) { thread.Start(); bool threadPoolThreadIsThreadPoolThread = false; thread.DoSynchronously(() => { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext())) { GenericSynchronizingObject test = new GenericSynchronizingObject(); IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { threadPoolThreadIsThreadPoolThread = Thread.CurrentThread.IsThreadPoolThread; }), null); test.EndInvoke(result); } }); Assert.IsTrue(threadPoolThreadIsThreadPoolThread, "ThreadPool thread is not a thread pool thread"); } }
public void Action_BeginInvokeThroughThreadPoolGSO_Runs() { using (ActionThread thread = new ActionThread()) { thread.Start(); bool sawAction = false; thread.DoSynchronously(() => { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext())) { GenericSynchronizingObject test = new GenericSynchronizingObject(); IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null); test.EndInvoke(result); } }); Assert.IsTrue(sawAction, "BeginInvoke did not execute action."); } }
public void FailingAction_InvokedThroughThreadPoolGSO_PreservesExceptionAsInnerException() { using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext())) { GenericSynchronizingObject test = new GenericSynchronizingObject(); Exception errorToThrow = new MyException(); Exception innerErrorCaught = null; try { test.Invoke((MethodInvoker)(() => { throw errorToThrow; }), null); } catch (TargetInvocationException ex) { innerErrorCaught = ex.InnerException; } Assert.AreSame(errorToThrow, innerErrorCaught, "Exception not preserved"); } }
public void FailingAction_InvokedThroughActionThreadGSO_PreservesExceptionAsInnerException() { using (ActionThread thread = new ActionThread()) { thread.Start(); // Capture GenericSynchronizingObject GenericSynchronizingObject test = thread.DoGet(() => { return(new GenericSynchronizingObject()); }); Exception errorToThrow = new MyException(); Exception innerErrorCaught = null; try { test.Invoke((MethodInvoker)(() => { throw errorToThrow; }), null); } catch (TargetInvocationException ex) { innerErrorCaught = ex.InnerException; } Assert.AreSame(errorToThrow, innerErrorCaught, "Exception not preserved"); } }
public void ThreadPoolGSO_Invoked_ExecutesSynchronously() { int threadId = ~Thread.CurrentThread.ManagedThreadId; using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext())) { GenericSynchronizingObject test = new GenericSynchronizingObject(); test.Invoke((MethodInvoker)(() => { threadId = Thread.CurrentThread.ManagedThreadId; }), null); } Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, threadId, "ThreadPool invoke did not operate synchronously"); }