Exemplo n.º 1
0
        public void AttachToDispatcher()
        {
            Dispatcher aDispatcher = WindowsDispatching.StartNewWindowsDispatcher();

            IThreadDispatcherProvider aDispatching       = new WindowsDispatching(aDispatcher);
            IThreadDispatcher         anEneterDispatcher = aDispatching.GetDispatcher();

            ManualResetEvent anInvokeCompleted1 = new ManualResetEvent(false);
            int aThreadId1 = 0;

            anEneterDispatcher.Invoke(() =>
            {
                aThreadId1 = Thread.CurrentThread.ManagedThreadId;
                anInvokeCompleted1.Set();
            });
            anInvokeCompleted1.WaitOne();

            ManualResetEvent anInvokeCompleted2 = new ManualResetEvent(false);
            int aThreadId2 = 0;

            anEneterDispatcher.Invoke(() =>
            {
                aThreadId2 = Thread.CurrentThread.ManagedThreadId;
                anInvokeCompleted2.Set();
            });
            anInvokeCompleted1.WaitOne();

            WindowsDispatching.StopWindowsDispatcher(aDispatcher);

            Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, aThreadId1);
            Assert.AreEqual(aThreadId1, aThreadId2);
        }
        public void ThreadDispatching()
        {
            IDuplexTypedMessageReceiver <string, int> aReceiver = DuplexTypedMessagesFactory.CreateDuplexTypedMessageReceiver <string, int>();

            aReceiver.MessageReceived += (x, y) =>
            {
                aReceiver.SendResponseMessage(y.ResponseReceiverId, (y.RequestMessage * 10).ToString());
            };

            // Set windows working thread dispatcher.
            Dispatcher         aWindowsDispatcher = WindowsDispatching.StartNewWindowsDispatcher();
            WindowsDispatching aThreadDispatching = new WindowsDispatching(aWindowsDispatcher);

            ((DuplexTypedMessagesFactory)DuplexTypedMessagesFactory).SyncDuplexTypedSenderThreadMode = aThreadDispatching;

            ISyncDuplexTypedMessageSender <string, int> aSender = DuplexTypedMessagesFactory.CreateSyncDuplexTypedMessageSender <string, int>();

            int aOpenConnectionThreadId = 0;

            aSender.ConnectionOpened += (x, y) =>
            {
                aOpenConnectionThreadId = Thread.CurrentThread.ManagedThreadId;
            };

            ManualResetEvent aConnectionClosedEvent = new ManualResetEvent(false);
            int aCloseConnectionThreadId            = 0;

            aSender.ConnectionClosed += (x, y) =>
            {
                aCloseConnectionThreadId = Thread.CurrentThread.ManagedThreadId;
                aConnectionClosedEvent.Set();
            };

            try
            {
                aReceiver.AttachDuplexInputChannel(InputChannel);
                aSender.AttachDuplexOutputChannel(OutputChannel);

                string aResult = aSender.SendRequestMessage(100);

                aReceiver.AttachedDuplexInputChannel.DisconnectResponseReceiver(aSender.AttachedDuplexOutputChannel.ResponseReceiverId);
                aConnectionClosedEvent.WaitOne();

                Assert.AreEqual("1000", aResult);
                Assert.AreEqual(aWindowsDispatcher.Thread.ManagedThreadId, aOpenConnectionThreadId);
                Assert.AreEqual(aWindowsDispatcher.Thread.ManagedThreadId, aCloseConnectionThreadId);
            }
            finally
            {
                aSender.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();

                if (aWindowsDispatcher != null)
                {
                    WindowsDispatching.StopWindowsDispatcher(aWindowsDispatcher);
                }
            }
        }