Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Refactored from Thread to Task and some Task patterns to Parallel #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions source/Appccelerate.EventBroker/Handlers/OnBackground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Appccelerate.EventBroker.Handlers
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

using Appccelerate.EventBroker.Internals.Subscriptions;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,7 @@ 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<IEventScope, IEventScope> ExecuteOnDifferentThreads(Func<IEventScope> firstThreadAction, Func<IEventScope> secondThreadAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ namespace Appccelerate.ScopingEventBroker.Internals.Context
{
using System;
using System.Threading;
using System.Threading.Tasks;

using FakeItEasy;

using FluentAssertions;

using Xunit;


public class PerThreadEventScopeContextTest : IDisposable
{
private readonly PerThreadEventScopeContext testee;
Expand Down Expand Up @@ -63,14 +65,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)
Expand Down