public void Start_OnStartedThread_ThrowsThreadStateException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Start();
     }
 }
Exemplo n.º 2
0
        public void Timer_SetTooBigInterval_IsRejected()
        {
            Exception ex = null;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    try
                    {
                        using (Timer timer = new Timer())
                        {
                            timer.Interval = TimeSpan.FromMilliseconds(((double)int.MaxValue) + 1);
                        }
                    }
                    catch (Exception error)
                    {
                        ex = error;
                    }
                });
            }

            Assert.IsNotNull(ex, "Timer.Interval should reject intervals larger than int.MaxValue");
        }
Exemplo n.º 3
0
        public void Timer_SetNegativeInterval_IsRejected()
        {
            Exception ex = null;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() =>
                {
                    try
                    {
                        using (Timer timer = new Timer())
                        {
                            timer.Interval = TimeSpan.FromMilliseconds(-1);
                        }
                    }
                    catch (Exception error)
                    {
                        ex = error;
                    }
                });
            }

            Assert.IsNotNull(ex, "Timer.Interval should reject negative intervals");
        }
Exemplo n.º 4
0
        public void PeriodicTimer_Elapsed_CanChangeInterval()
        {
            TimeSpan interval = default(TimeSpan);

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer = new Timer();
                    timer.Elapsed += () =>
                    {
                        timer.Interval = TimeSpan.FromMilliseconds(1);
                        interval = timer.Interval;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval = TimeSpan.FromMilliseconds(0);
                    timer.Enabled = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(TimeSpan.FromMilliseconds(1), interval, "Interval should be honored when called from Elapsed");
        }
Exemplo n.º 5
0
        public void PeriodicTimer_Elapsed_CanCancelTimer()
        {
            int actionCount = 0;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        ++actionCount;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(1, actionCount, "Timer did not honor Cancel when called from Elapsed");
        }
        public void SyncedActionWithFourArguments_Invoked_ReceivesParameters()
        {
            int arg1 = 0;
            int arg2 = 0;
            int arg3 = 0;
            int arg4 = 0;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Have the ActionThread set "action"
                Action <int, int, int, int> action = thread.DoGet(() =>
                {
                    return(Sync.SynchronizeAction((int a1, int a2, int a3, int a4) => { arg1 = a1; arg2 = a2; arg3 = a3; arg4 = a4; }));
                });

                action(13, 17, 19, 23);
                thread.Join();

                Assert.AreEqual(13, arg1, "Action did not receive parameter");
                Assert.AreEqual(17, arg2, "Action did not receive parameter");
                Assert.AreEqual(19, arg3, "Action did not receive parameter");
                Assert.AreEqual(23, arg4, "Action did not receive parameter");
            }
        }
        public void Send_ToCopiedDispatcher_IsSynchronous()
        {
            SynchronizationContext actionDispatcherSyncContext = null;

            using (ManualResetEvent completed = new ManualResetEvent(false))
                using (ManualResetEvent wait = new ManualResetEvent(false))
                    using (ActionThread thread1 = new ActionThread())
                        using (ActionThread thread2 = new ActionThread())
                        {
                            thread1.Start();
                            thread2.Start();

                            // Capture the second thread's SynchronizationContext and signal this thread when it's captured.
                            actionDispatcherSyncContext = thread2.DoGet(() => { return(SynchronizationContext.Current.CreateCopy()); });

                            // Have the first thread do a synchronous Send to the second thread and then trigger the "completed" event.
                            // The action queued to the second thread will wait for the "wait" event.

                            thread1.Do(() =>
                            {
                                actionDispatcherSyncContext.Send((state) => { wait.WaitOne(); }, null);
                                completed.Set();
                            });

                            bool completedSignalled = completed.WaitOne(100);
                            Assert.IsFalse(completedSignalled, "ActionDispatcherSynchronizationContext.Send is not synchronous");

                            wait.Set();
                        }
        }
        public void Post_ToCopiedDispatcher_IsAsynchronous()
        {
            SynchronizationContext actionDispatcherSyncContext = null;

            using (ManualResetEvent completed = new ManualResetEvent(false))
            using (ManualResetEvent wait = new ManualResetEvent(false))
            using (ActionThread thread1 = new ActionThread())
            using (ActionThread thread2 = new ActionThread())
            {
                thread1.Start();
                thread2.Start();

                // Capture the second thread's SynchronizationContext and signal this thread when it's captured.
                actionDispatcherSyncContext = thread2.DoGet(() => { return SynchronizationContext.Current.CreateCopy(); });

                // Have the first thread do an synchronous Post to the second thread and then trigger the "completed" event.
                // The action queued to the second thread will wait for the "wait" event.

                thread1.Do(() =>
                {
                    actionDispatcherSyncContext.Post((state) => { wait.WaitOne(); }, null);
                    completed.Set();
                });

                bool completedSignalled = completed.WaitOne(100);
                Assert.IsTrue(completedSignalled, "ActionDispatcherSynchronizationContext.Post is not asynchronous");

                wait.Set();
            }
        }
Exemplo n.º 9
0
        public void SingleShotTimer_Elapsed_CanRestartTimer()
        {
            int actionCount = 0;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        if (actionCount == 0)
                        {
                            timer.Restart();
                        }
                        ++actionCount;
                    };
                    timer.AutoReset = false;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(2, actionCount, "Timer did not honor Restart when called from Elapsed");
        }
Exemplo n.º 10
0
        public void PeriodicTimer_Elapsed_CanChangeToSingleShot()
        {
            bool autoReset = true;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        timer.SetSingleShot(timer.Interval);
                        autoReset = timer.AutoReset;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.IsFalse(autoReset, "Periodic Timer should be able to change to Single-shot within Elapsed");
        }
Exemplo n.º 11
0
        public void PeriodicTimer_Elapsed_CanChangeInterval()
        {
            TimeSpan interval = default(TimeSpan);

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        timer.Interval = TimeSpan.FromMilliseconds(1);
                        interval       = timer.Interval;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(TimeSpan.FromMilliseconds(1), interval, "Interval should be honored when called from Elapsed");
        }
Exemplo n.º 12
0
        public void PeriodicTimer_Elapsed_CanCancelTimer()
        {
            int actionCount = 0;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer = new Timer();
                    timer.Elapsed += () =>
                    {
                        ++actionCount;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval = TimeSpan.FromMilliseconds(0);
                    timer.Enabled = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.AreEqual(1, actionCount, "Timer did not honor Cancel when called from Elapsed");
        }
        public void InvalidBoundSyncedFunc_Invoked_DoesSync()
        {
            bool sawSync = false;

            using (CallbackContext context = new CallbackContext())
                using (ActionThread thread = new ActionThread())
                {
                    thread.Start();

                    // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                    SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return(SynchronizationContext.Current); });

                    var syncContext = new Util.LoggingSynchronizationContext(actionThreadSyncContext)
                    {
                        OnPost = () => { sawSync = true; },
                        OnSend = () => { sawSync = true; }
                    };

                    var action = context.Bind(() => { return(13); }, syncContext, false);
                    context.Reset();
                    int result = action();

                    Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
                }
        }
        public void BoundAsyncAction_Invoked_UsesSyncContext()
        {
            bool sawSync = false;

            using (CallbackContext context = new CallbackContext())
                using (ActionThread thread = new ActionThread())
                {
                    thread.Start();

                    // Capture the thread's SynchronizationContext
                    SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return(SynchronizationContext.Current); });

                    var syncContext = new Util.LoggingSynchronizationContext(actionThreadSyncContext)
                    {
                        OnPost = () => { sawSync = true; },
                        OnSend = () => { sawSync = true; }
                    };

                    var action = context.AsyncBind(() => { }, syncContext, false);
                    action();
                    thread.Join();

                    Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
                }
        }
Exemplo n.º 15
0
        public void PeriodicTimer_Elapsed_IsEnabled()
        {
            bool enabled = false;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                Timer timer = null;
                thread.DoSynchronously(() =>
                {
                    timer          = new Timer();
                    timer.Elapsed += () =>
                    {
                        enabled = timer.Enabled;
                        timer.Cancel();
                    };
                    timer.AutoReset = true;
                    timer.Interval  = TimeSpan.FromMilliseconds(0);
                    timer.Enabled   = true;
                });
                Thread.Sleep(10);
                thread.DoSynchronously(() => timer.Dispose());
            }

            Assert.IsTrue(enabled, "Periodic Timer should be enabled when called from Elapsed");
        }
 public void Join_WithoutActions_Joins()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Join();
     }
 }
 public void NameSet_AfterStart_ThrowsInvalidOperationException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Name = "Bob";
     }
 }
 public void Name_OnMultipleSets_ThrowsInvalidOperationException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Name = "Bob";
         thread.Name = "Sue";
     }
 }
        public void IsAliveProperty_AfterStart_IsTrue()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                Assert.IsTrue(thread.IsAlive, "ActionThread is not alive after starting");
            }
        }
 public void SyncAction_QueuedAfterJoin_ThrowsThreadStateException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Join();
         thread.DoSynchronously(() => { });
     }
 }
 public void JoinWithTimeout_WithoutActions_Joins()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         bool signalled = thread.Join(TimeSpan.FromMilliseconds(100));
         Assert.IsTrue(signalled, "ActionThread did not Join");
     }
 }
 public void PrioritySet_AfterStart_RemembersValue()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Priority = ThreadPriority.BelowNormal;
         Assert.AreEqual(ThreadPriority.BelowNormal, thread.Priority, "ActionThread did not remember Priority");
     }
 }
Exemplo n.º 23
0
        public void IsAliveProperty_AfterStart_IsTrue()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                Assert.IsTrue(thread.IsAlive, "ActionThread is not alive after starting");
            }
        }
        public void IsBackgroundProperty_AfterStart_CanBeSetToTrue()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                thread.IsBackground = true;
                Assert.IsTrue(thread.IsBackground, "ActionThread did not remember IsBackground");
            }
        }
        public void SyncFunc_QueuedAfterStart_PreservesReturnValue()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                object obj = thread.DoGet(() => { return(new object()); });
                Assert.IsNotNull(obj, "ActionThread did not return result");
            }
        }
Exemplo n.º 26
0
        public static NSThread Start(NSAction action)
        {
            if (action == null) {
                throw new ArgumentNullException ("action");
            }

            var thread = new ActionThread (action);
            thread.Start ();
            return thread;
        }
        public void IsAliveProperty_AfterStartAndJoin_IsFalse()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.Join();

                Assert.IsFalse(thread.IsAlive, "ActionThread is alive after joining");
            }
        }
        public void NameSet_ReadAfterStart_RemembersValue()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Name = "Bob";
                thread.Start();

                Assert.AreEqual("Bob", thread.Name, "ActionThread did not remember name after joining");
            }
        }
Exemplo n.º 29
0
        public bool Start()
        {
            ActionThread.Start();
            TimeoutThread.Start();

            ThreadSynchronizer.WaitOne();

            ThreadSynchronizer.Close();
            return(_success);
        }
Exemplo n.º 30
0
        public void Dispose_WithoutExplicitJoin_Joins()
        {
            ActionThread thread = new ActionThread();
            using (thread)
            {
                thread.Start();
            }

            Assert.IsFalse(thread.IsAlive, "ActionThread did not implicitly join");
        }
Exemplo n.º 31
0
        public void IsAliveProperty_AfterStartAndJoin_IsFalse()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.Join();

                Assert.IsFalse(thread.IsAlive, "ActionThread is alive after joining");
            }
        }
        public void Dispose_WithoutExplicitJoin_Joins()
        {
            ActionThread thread = new ActionThread();

            using (thread)
            {
                thread.Start();
            }

            Assert.IsFalse(thread.IsAlive, "ActionThread did not implicitly join");
        }
    static void Main()
    {
        using (ActionThread actionThread = new ActionThread())
        {
            actionThread.Start();

            // This call to Join is not strictly necessary, since Dispose
            //  will perform an implicit Join.
            actionThread.Join();
        }
    }
 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");
         }
 }
Exemplo n.º 35
0
    static void Main()
    {
        using (ActionThread actionThread = new ActionThread())
        {
            actionThread.Start();

            // Dispose performs an implicit Join at the end of this block,
            //  which waits for the ActionThread to finish its current queue
            //  of actions.
        }
    }
        public void NameDefaultValue_ReadAfterStart_IsNitoActionThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool containsNito         = thread.Name.Contains("Nito");
                bool containsActionThread = thread.Name.Contains("ActionThread");
                Assert.IsTrue(containsNito && containsActionThread, "ActionThread did not default to a decent name when starting");
            }
        }
        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 ActionThreadGSO_WithinActionThread_DoesNotRequireInvoke()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture InvokeRequired from the ActionThread's context
                bool nestedInvokeRequired = thread.DoGet(() => { return new GenericSynchronizingObject().InvokeRequired; });

                Assert.IsFalse(nestedInvokeRequired, "GenericSynchronizingObject does require invoke within ActionThread");
            }
        }
        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");
            }
        }
Exemplo n.º 40
0
        public void Action_QueuedBeforeStart_IsExecutedByThread()
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            using (ActionThread thread = new ActionThread())
            {
                thread.Do(() => threadId = Thread.CurrentThread.ManagedThreadId);
                thread.Start();
                thread.Join();

                Assert.AreEqual(thread.ManagedThreadId, threadId, "ActionThread ran in wrong thread context");
            }
        }
        public void Action_BeginInvokeThroughActionThreadGSO_RunsOnTheActionThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture GenericSynchronizingObject
                GenericSynchronizingObject test = thread.DoGet(() => { return new GenericSynchronizingObject(); });

                int actionThreadId = Thread.CurrentThread.ManagedThreadId;
                IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { actionThreadId = Thread.CurrentThread.ManagedThreadId; }), null);
                test.EndInvoke(result);

                Assert.AreEqual(thread.ManagedThreadId, actionThreadId, "GenericSynchronizingObject.BeginInvoke did not synchronize");
            }
        }
        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");
            }
        }
Exemplo n.º 43
0
        public void ActionWithTwoArguments_AfterSync_HasNotRun()
        {
            bool sawAction = false;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Have the ActionThread set "action"
                Action<int, int> action = thread.DoGet(() =>
                {
                    return Sync.SynchronizeAction((int a1, int a2) => { sawAction = true; });
                });

                // The action should be run in the context of the ActionThread
                Assert.IsFalse(sawAction, "Action should not have run already");
            }
        }
Exemplo n.º 44
0
        public GameSocketManager Start()
        {
            _actionThread = new ActionThread
                                {
                                    Name = "BLUEDOT-GameSocketThread"
                                };

            _actionThread.Start();
            _actionThread.Do(() =>
            {
                _listeningSocket = new ServerTcpSocket();
                _listeningSocket.AcceptCompleted += IncomingConnectedAccepted;
                _listeningSocket.Bind(Address, Port);
                _listeningSocket.AcceptAsync();
            });

            return this;
        }
        public void BoundAsyncAction_Invoked_DecrementsSyncContextOperationCount()
        {
            bool sawOperationCompleted = false;

            using (CallbackContext context = new CallbackContext())
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var syncContext = new LoggingSynchronizationContext(actionThreadSyncContext)
                {
                    OnOperationCompleted = () => { sawOperationCompleted = true; }
                };

                var action = context.AsyncBind(() => { }, syncContext, false);
                action();
                thread.Join();

                Assert.IsFalse(sawOperationCompleted, "Context decremented operation count");
            }
        }
Exemplo n.º 46
0
 public void PrioritySet_AfterStart_RemembersValue()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Priority = ThreadPriority.BelowNormal;
         Assert.AreEqual(ThreadPriority.BelowNormal, thread.Priority, "ActionThread did not remember Priority");
     }
 }
Exemplo n.º 47
0
 public void Start_OnStartedThread_ThrowsThreadStateException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Start();
     }
 }
Exemplo n.º 48
0
 public void SyncAction_QueuedAfterJoin_ThrowsThreadStateException()
 {
     using (ActionThread thread = new ActionThread())
     {
         thread.Start();
         thread.Join();
         thread.DoSynchronously(() => { });
     }
 }
Exemplo n.º 49
0
        public void SyncAction_QueuedAfterStart_IsExecutedByThread()
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                thread.DoSynchronously(() => { threadId = Thread.CurrentThread.ManagedThreadId; });

                Assert.AreEqual(thread.ManagedThreadId, threadId, "ActionThread ran in wrong thread context");
            }
        }
Exemplo n.º 50
0
        public void SyncContext_FromInsideAction_IsActionDispatcherSyncContext()
        {
            SynchronizationContext actionThreadSyncContext = null;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                Assert.IsInstanceOfType(actionThreadSyncContext, typeof(ActionDispatcherSynchronizationContext), "ActionThread did not provide an ActionDispatcherSynchronizationContext");
            }
        }
        public void InvalidBoundSyncedFunc_Invoked_ReturnsDefault()
        {
            using (CallbackContext context = new CallbackContext())
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var action = context.Bind(() => { return 13; }, actionThreadSyncContext, false);
                context.Reset();
                int result = action();

                Assert.AreEqual(0, result, "Invalid Func returned a non-default value");
            }
        }
Exemplo n.º 52
0
        public void SyncFunc_QueuedAfterStart_IsExecutedByThread()
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();
                object obj = thread.DoGet(() => { threadId = Thread.CurrentThread.ManagedThreadId; return new object(); });

                Assert.AreEqual(thread.ManagedThreadId, threadId, "ActionThread ran in wrong thread context");
            }
        }
        public void InvalidBoundSyncedFunc_Invoked_DoesNotExecute()
        {
            int sawActionThread = Thread.CurrentThread.ManagedThreadId;

            using (CallbackContext context = new CallbackContext())
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var action = context.Bind(() => { sawActionThread = Thread.CurrentThread.ManagedThreadId; return 13; }, actionThreadSyncContext, false);
                context.Reset();
                int result = action();

                Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, sawActionThread, "Invalid action should not run");
            }
        }
        public void BoundAsyncAction_Invoked_ExecutesSynchronized()
        {
            int sawActionThread = Thread.CurrentThread.ManagedThreadId;

            using (CallbackContext context = new CallbackContext())
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var action = context.AsyncBind(() => { sawActionThread = Thread.CurrentThread.ManagedThreadId; }, actionThreadSyncContext);
                action();
                thread.Join();

                Assert.AreEqual(thread.ManagedThreadId, sawActionThread, "Bound action was not synchronized");
            }
        }
        public void BoundSyncedFunc_Invoked_IncrementsSyncContextOperationCount()
        {
            bool sawOperationStarted = false;

            using (CallbackContext context = new CallbackContext())
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var syncContext = new LoggingSynchronizationContext(actionThreadSyncContext)
                {
                    OnOperationStarted = () => { sawOperationStarted = true; }
                };

                var action = context.Bind(() => { return 13; }, syncContext, false);
                int result = action();

                Assert.IsFalse(sawOperationStarted, "Context incremented operation count");
            }
        }
        public void BoundSyncedAction_Invoked_UsesSyncContext()
        {
            bool sawSync = false;

            using (CallbackContext context = new CallbackContext())
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var syncContext = new LoggingSynchronizationContext(actionThreadSyncContext)
                {
                    OnPost = () => { sawSync = true; },
                    OnSend = () => { sawSync = true; }
                };

                var action = context.Bind(() => { }, syncContext, false);
                action();

                Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
            }
        }
Exemplo n.º 57
0
        public void SyncContext_FromInsideAction_SendsToSameThread()
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                // Use the SynchronizationContext to give the ActionThread more work to do
                actionThreadSyncContext.Send((state) => { threadId = Thread.CurrentThread.ManagedThreadId; }, null);

                Assert.AreEqual(thread.ManagedThreadId, threadId, "ActionThread ran in wrong thread context");
            }
        }
        public void Send_IsSynchronous()
        {
            SynchronizationContext actionDispatcherSyncContext = null;

            using (ManualResetEvent completed = new ManualResetEvent(false))
            using (ManualResetEvent wait = new ManualResetEvent(false))
            using (ActionThread thread1 = new ActionThread())
            using (ActionThread thread2 = new ActionThread())
            {
                thread1.Start();
                thread2.Start();

                // Capture the second thread's SynchronizationContext.
                actionDispatcherSyncContext = thread2.DoGet(() => { return SynchronizationContext.Current; });

                // Have the first thread do a synchronous Send to the second thread and then trigger the "completed" event.
                // The action queued to the second thread will wait for the "wait" event.

                thread1.Do(() =>
                    {
                        actionDispatcherSyncContext.Send((state) => { wait.WaitOne(); }, null);
                        completed.Set();
                    });

                bool completedSignalled = completed.WaitOne(100);
                Assert.IsFalse(completedSignalled, "ActionDispatcherSynchronizationContext.Send is not synchronous");

                wait.Set();
            }
        }
        public void InvalidBoundSyncedFunc_Invoked_DoesSync()
        {
            bool sawSync = false;

            using (CallbackContext context = new CallbackContext())
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                // Capture the thread's SynchronizationContext and signal this thread when it's captured.
                SynchronizationContext actionThreadSyncContext = thread.DoGet(() => { return SynchronizationContext.Current; });

                var syncContext = new LoggingSynchronizationContext(actionThreadSyncContext)
                {
                    OnPost = () => { sawSync = true; },
                    OnSend = () => { sawSync = true; }
                };

                var action = context.Bind(() => { return 13; }, syncContext, false);
                context.Reset();
                int result = action();

                Assert.IsTrue(sawSync, "Context did not use SyncContext for sync");
            }
        }
Exemplo n.º 60
0
        public void SyncFunc_QueuedAfterStart_PreservesReturnValue()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                object obj = thread.DoGet(() => { return new object(); });
                Assert.IsNotNull(obj, "ActionThread did not return result");
            }
        }