From 7b1005fc07d3efb66af0d945afcbae0ba74811c7 Mon Sep 17 00:00:00 2001 From: Semih Okur Date: Sun, 8 Dec 2013 12:53:16 -0600 Subject: [PATCH 1/3] Refactored from Task patterns to Parallel class --- .../PerThreadScopeSpecifications.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/source/Appccelerate.ScopingEventBroker.Specification/PerThreadScopeSpecifications.cs b/source/Appccelerate.ScopingEventBroker.Specification/PerThreadScopeSpecifications.cs index d0a9f61..b41459b 100644 --- a/source/Appccelerate.ScopingEventBroker.Specification/PerThreadScopeSpecifications.cs +++ b/source/Appccelerate.ScopingEventBroker.Specification/PerThreadScopeSpecifications.cs @@ -273,18 +273,12 @@ public class PerThreadScopeSpecification : ScopingEventBrokerSpecification protected static void ExecuteOnDifferentThreads(Action firstThreadAction, Action secondThreadAction) { - var firstScopeTask = Task.Factory.StartNew(firstThreadAction); - var secondScopeTask = Task.Factory.StartNew(secondThreadAction); - - Task.WaitAll(firstScopeTask, secondScopeTask); + Parallel.Invoke(firstThreadAction, secondThreadAction); } protected static Tuple ExecuteOnDifferentThreads(Func firstThreadAction, Func secondThreadAction) { - var firstScopeTask = Task.Factory.StartNew(firstThreadAction); - var secondScopeTask = Task.Factory.StartNew(secondThreadAction); - - Task.WaitAll(firstScopeTask, secondScopeTask); + Parallel.Invoke(firstThreadAction, secondThreadAction); return new Tuple(firstScopeTask.Result, secondScopeTask.Result); } From f9d233f49aececf3b160c1ca7610d18bbafa5083 Mon Sep 17 00:00:00 2001 From: Semih Okur Date: Sun, 8 Dec 2013 12:58:50 -0600 Subject: [PATCH 2/3] Refactored from Thread, ThreadPool to Task --- .../Appccelerate.EventBroker/Handlers/OnBackground.cs | 9 ++++----- .../PerThreadScopeSpecifications.cs | 5 ++++- .../Context/PerThreadEventScopeContextTest.cs | 10 +++++----- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/source/Appccelerate.EventBroker/Handlers/OnBackground.cs b/source/Appccelerate.EventBroker/Handlers/OnBackground.cs index ab2987e..168c244 100644 --- a/source/Appccelerate.EventBroker/Handlers/OnBackground.cs +++ b/source/Appccelerate.EventBroker/Handlers/OnBackground.cs @@ -23,6 +23,7 @@ namespace Appccelerate.EventBroker.Handlers using System.Threading; using Appccelerate.EventBroker.Internals.Subscriptions; + using System.Threading.Tasks; /// /// Handler that executes the subscription on a thread pool worker process (asynchronous). @@ -43,20 +44,18 @@ public override HandlerKind Kind public override void Handle(IEventTopicInfo eventTopic, object subscriber, object sender, EventArgs e, IDelegateWrapper delegateWrapper) { - ThreadPool.QueueUserWorkItem( - delegate(object state) + Task.Run(()=> { try { - var args = (CallInBackgroundArguments)state; + var args = new CallInBackgroundArguments(subscriber, sender, e, delegateWrapper); args.DelegateWrapper.Invoke(args.Subscriber, args.Sender, args.EventArgs); } catch (TargetInvocationException exception) { this.HandleSubscriberMethodException(exception, eventTopic); } - }, - new CallInBackgroundArguments(subscriber, sender, e, delegateWrapper)); + }); } private struct CallInBackgroundArguments diff --git a/source/Appccelerate.ScopingEventBroker.Specification/PerThreadScopeSpecifications.cs b/source/Appccelerate.ScopingEventBroker.Specification/PerThreadScopeSpecifications.cs index b41459b..668988b 100644 --- a/source/Appccelerate.ScopingEventBroker.Specification/PerThreadScopeSpecifications.cs +++ b/source/Appccelerate.ScopingEventBroker.Specification/PerThreadScopeSpecifications.cs @@ -278,7 +278,10 @@ protected static void ExecuteOnDifferentThreads(Action firstThreadAction, Action protected static Tuple ExecuteOnDifferentThreads(Func firstThreadAction, Func secondThreadAction) { - Parallel.Invoke(firstThreadAction, secondThreadAction); + var firstScopeTask = Task.Factory.StartNew(firstThreadAction); + var secondScopeTask = Task.Factory.StartNew(secondThreadAction); + + Task.WaitAll(firstScopeTask, secondScopeTask); return new Tuple(firstScopeTask.Result, secondScopeTask.Result); } diff --git a/source/Appccelerate.ScopingEventBroker.Test/Internals/Context/PerThreadEventScopeContextTest.cs b/source/Appccelerate.ScopingEventBroker.Test/Internals/Context/PerThreadEventScopeContextTest.cs index c346942..35830a8 100644 --- a/source/Appccelerate.ScopingEventBroker.Test/Internals/Context/PerThreadEventScopeContextTest.cs +++ b/source/Appccelerate.ScopingEventBroker.Test/Internals/Context/PerThreadEventScopeContextTest.cs @@ -26,6 +26,7 @@ namespace Appccelerate.ScopingEventBroker.Internals.Context using FluentAssertions; using Xunit; + using System.Threading.Tasks; public class PerThreadEventScopeContextTest : IDisposable { @@ -63,14 +64,13 @@ public void Acquire_WhenDifferentThread_ReturnsNew() IEventScope firstScopeTaskResult = null; IEventScope secondScopeTaskResult = null; - var firstScopeTask = new Thread(() => firstScopeTaskResult = this.testee.Acquire()); - var secondScopeTask = new Thread(() => secondScopeTaskResult = this.testee.Acquire()); + var firstScopeTask = Task.Run(() => firstScopeTaskResult = this.testee.Acquire()); + var secondScopeTask = new Task(() => secondScopeTaskResult = this.testee.Acquire()); - firstScopeTask.Start(); - firstScopeTask.Join(); + firstScopeTask.Wait(); secondScopeTask.Start(); - secondScopeTask.Join(); + secondScopeTask.Wait(); using (IEventScope firstScope = firstScopeTaskResult) using (IEventScope secondScope = secondScopeTaskResult) From e17327afabc6c6af0ab9ad1bcd4115269353ecba Mon Sep 17 00:00:00 2001 From: Semih Okur Date: Sun, 8 Dec 2013 16:16:00 -0600 Subject: [PATCH 3/3] ordered the namespaces --- source/Appccelerate.EventBroker/Handlers/OnBackground.cs | 2 +- .../Internals/Context/PerThreadEventScopeContextTest.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/Appccelerate.EventBroker/Handlers/OnBackground.cs b/source/Appccelerate.EventBroker/Handlers/OnBackground.cs index 168c244..aa7589f 100644 --- a/source/Appccelerate.EventBroker/Handlers/OnBackground.cs +++ b/source/Appccelerate.EventBroker/Handlers/OnBackground.cs @@ -21,9 +21,9 @@ namespace Appccelerate.EventBroker.Handlers using System; using System.Reflection; using System.Threading; + using System.Threading.Tasks; using Appccelerate.EventBroker.Internals.Subscriptions; - using System.Threading.Tasks; /// /// Handler that executes the subscription on a thread pool worker process (asynchronous). diff --git a/source/Appccelerate.ScopingEventBroker.Test/Internals/Context/PerThreadEventScopeContextTest.cs b/source/Appccelerate.ScopingEventBroker.Test/Internals/Context/PerThreadEventScopeContextTest.cs index 35830a8..b94a674 100644 --- a/source/Appccelerate.ScopingEventBroker.Test/Internals/Context/PerThreadEventScopeContextTest.cs +++ b/source/Appccelerate.ScopingEventBroker.Test/Internals/Context/PerThreadEventScopeContextTest.cs @@ -20,13 +20,14 @@ namespace Appccelerate.ScopingEventBroker.Internals.Context { using System; using System.Threading; + using System.Threading.Tasks; using FakeItEasy; using FluentAssertions; using Xunit; - using System.Threading.Tasks; + public class PerThreadEventScopeContextTest : IDisposable {