Updated Mono project (may replace it with pure SCons or Gradle build soon); added a threaded task runner I wrote for some WPF applications going through MCSE; wrote more unit tests for the thread runner

git-svn-id: file:///srv/devel/repo-conversion/nusu@332 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2019-02-02 09:12:00 +00:00
parent e6e0220fb3
commit 2f82a2fdf9
5 changed files with 995 additions and 8 deletions

View File

@ -48,6 +48,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\Async\AsyncStatus.cs" />
<Compile Include="Source\Async\AsyncStatusEventArgs.cs" />
<Compile Include="Source\Async\IAsyncAction.cs" />
<Compile Include="Source\Async\IAsyncSwitch.cs" />
<Compile Include="Source\Async\IAsyncTask.cs" />
<Compile Include="Source\Async\ICancellable.cs" />
<Compile Include="Source\Async\IProgressSource.cs" />
<Compile Include="Source\Cloning\CloneFactoryTest.cs" /> <Compile Include="Source\Cloning\CloneFactoryTest.cs" />
<Compile Include="Source\Cloning\ExpressionTreeCloner.cs" /> <Compile Include="Source\Cloning\ExpressionTreeCloner.cs" />
<Compile Include="Source\Cloning\ExpressionTreeCloner.FieldBased.cs"> <Compile Include="Source\Cloning\ExpressionTreeCloner.FieldBased.cs">
@ -235,10 +242,6 @@
<Compile Include="Source\Parsing\CommandLine.Parser.cs"> <Compile Include="Source\Parsing\CommandLine.Parser.cs">
<DependentUpon>CommandLine.cs</DependentUpon> <DependentUpon>CommandLine.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\ParallelBackgroundWorker.cs" />
<Compile Include="Source\ParallelBackgroundWorker.Test.cs">
<DependentUpon>ParallelBackgroundWorker.cs</DependentUpon>
</Compile>
<Compile Include="Source\Parsing\ParserHelper.cs" /> <Compile Include="Source\Parsing\ParserHelper.cs" />
<Compile Include="Source\Parsing\ParserHelper.Test.cs"> <Compile Include="Source\Parsing\ParserHelper.Test.cs">
<DependentUpon>ParserHelper.cs</DependentUpon> <DependentUpon>ParserHelper.cs</DependentUpon>
@ -247,10 +250,6 @@
<Compile Include="Source\PropertyChangedEventArgsHelper.Test.cs"> <Compile Include="Source\PropertyChangedEventArgsHelper.Test.cs">
<DependentUpon>PropertyChangedEventArgsHelper.cs</DependentUpon> <DependentUpon>PropertyChangedEventArgsHelper.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\AffineThreadPool.cs" />
<Compile Include="Source\AffineThreadPool.Test.cs">
<DependentUpon>AffineThreadPool.cs</DependentUpon>
</Compile>
<Compile Include="Source\EnumHelper.cs" /> <Compile Include="Source\EnumHelper.cs" />
<Compile Include="Source\EnumHelper.Test.cs"> <Compile Include="Source\EnumHelper.Test.cs">
<DependentUpon>EnumHelper.cs</DependentUpon> <DependentUpon>EnumHelper.cs</DependentUpon>
@ -263,6 +262,19 @@
<Compile Include="Source\ObservableHelper.Test.cs"> <Compile Include="Source\ObservableHelper.Test.cs">
<DependentUpon>ObservableHelper.cs</DependentUpon> <DependentUpon>ObservableHelper.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Threading\AffineThreadPool.cs" />
<Compile Include="Source\Threading\AffineThreadPool.Test.cs">
<DependentUpon>AffineThreadPool.cs</DependentUpon>
</Compile>
<Compile Include="Source\Threading\CancellableAction.cs" />
<Compile Include="Source\Threading\ParallelBackgroundWorker.cs" />
<Compile Include="Source\Threading\ParallelBackgroundWorker.Test.cs">
<DependentUpon>ParallelBackgroundWorker.cs</DependentUpon>
</Compile>
<Compile Include="Source\Threading\ThreadRunner.cs" />
<Compile Include="Source\Threading\ThreadRunner.Test.cs">
<DependentUpon>ThreadRunner.cs</DependentUpon>
</Compile>
<Compile Include="Source\TypeHelper.cs" /> <Compile Include="Source\TypeHelper.cs" />
<Compile Include="Source\TypeHelper.Test.cs"> <Compile Include="Source\TypeHelper.Test.cs">
<DependentUpon>TypeHelper.cs</DependentUpon> <DependentUpon>TypeHelper.cs</DependentUpon>

View File

@ -275,10 +275,15 @@
<Compile Include="Source\Threading\AffineThreadPool.Test.cs"> <Compile Include="Source\Threading\AffineThreadPool.Test.cs">
<DependentUpon>AffineThreadPool.cs</DependentUpon> <DependentUpon>AffineThreadPool.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Threading\CancellableAction.cs" />
<Compile Include="Source\Threading\ParallelBackgroundWorker.cs" /> <Compile Include="Source\Threading\ParallelBackgroundWorker.cs" />
<Compile Include="Source\Threading\ParallelBackgroundWorker.Test.cs"> <Compile Include="Source\Threading\ParallelBackgroundWorker.Test.cs">
<DependentUpon>ParallelBackgroundWorker.cs</DependentUpon> <DependentUpon>ParallelBackgroundWorker.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Threading\ThreadRunner.cs" />
<Compile Include="Source\Threading\ThreadRunner.Test.cs">
<DependentUpon>ThreadRunner.cs</DependentUpon>
</Compile>
<Compile Include="Source\TypeHelper.cs" /> <Compile Include="Source\TypeHelper.cs" />
<Compile Include="Source\TypeHelper.Test.cs"> <Compile Include="Source\TypeHelper.Test.cs">
<DependentUpon>TypeHelper.cs</DependentUpon> <DependentUpon>TypeHelper.cs</DependentUpon>

View File

@ -0,0 +1,49 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Threading;
namespace Nuclex.Support.Threading {
/// <summary>Action with no arguments that can be cancelled</summary>
/// <param name="cancellationToken">
/// Cancellation token by which the action can be cancelled
/// </param>
public delegate void CancellableAction(CancellationToken cancellationToken);
/// <summary>Action with no arguments that can be cancelled</summary>
/// <param name="cancellationToken">
/// Cancellation token by which the action can be cancelled
/// </param>
/// <param name="arg1">First argument for the action</param>
public delegate void CancellableAction<in T1>(T1 arg1, CancellationToken cancellationToken);
/// <summary>Action with no arguments that can be cancelled</summary>
/// <param name="cancellationToken">
/// Cancellation token by which the action can be cancelled
/// </param>
/// <param name="arg1">First argument for the action</param>
/// <param name="arg2">Second argument for the action</param>
public delegate void CancellableAction<in T1, in T2>(
T1 arg1, T2 arg2, CancellationToken cancellationToken
);
} // namespace Nuclex.Support.Threading

View File

@ -0,0 +1,460 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2017 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if !NO_CONCURRENT_COLLECTIONS
using System;
using System.Threading;
using System.Collections.Generic;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Threading {
/// <summary>Unit Test for the thread runner class</summary>
[TestFixture]
internal class ThreadRunnerTest {
#region class DefaultDisposeRunner
/// <summary>Implementation of a thread runner to check default dispose behavior</summary>
private class DefaultDisposeRunner : ThreadRunner {
/// <summary>Reports an error</summary>
/// <param name="exception">Error that will be reported</param>
protected override void ReportError(Exception exception) { }
/// <summary>Called when the status of the busy flag changes</summary>
protected override void BusyChanged() { }
}
#endregion // class DefaultDisposeRunner
#region class DummyRunner
/// <summary>Implementation of a thread runner used for unit testing</summary>
private class DummyRunner : ThreadRunner {
/// <summary>Initializes a new dummy thread runner</summary>
public DummyRunner() : base() {
this.completionGate = new ManualResetEvent(initialState: false);
}
/// <summary>Immediately frees all resources used by the instance</summary>
public new void Dispose() {
base.Dispose(100);
if(this.completionGate != null) {
this.completionGate.Dispose();
this.completionGate = null;
}
}
/// <summary>Waits for the task for complete (all of 100 milliseconds)</summary>
/// <returns>True if the task completed, false if it continues running</returns>
public bool WaitForCompletion() {
return this.completionGate.WaitOne(100);
}
/// <summary>How often the status of the busy flag has changed</summary>
public int BusyChangeCount {
get { return this.busyChangeCount; }
}
/// <summary>Error that has been reported the last time a task was run</summary>
public Exception ReportedError {
get { return this.reportedError; }
}
/// <summary>Reports an error</summary>
/// <param name="exception">Error that will be reported</param>
protected override void ReportError(Exception exception) {
this.reportedError = exception;
}
/// <summary>Called when the status of the busy flag changes</summary>
protected override void BusyChanged() {
++busyChangeCount;
if((busyChangeCount >= 2) && (base.IsBusy == false)) {
this.completionGate.Set();
}
}
/// <summary>Last error that was reported in the thread</summary>
private Exception reportedError;
/// <summary>Number of times the busy state of the runner has changed</summary>
private int busyChangeCount;
/// <summary>Triggered when the busy event has performed a double flank</summary>
private ManualResetEvent completionGate;
}
#endregion // class DummyRunner
#region class DummyTask
/// <summary>Dummy task that can be executed by a thread runner</summary>
private class DummyTask : IDisposable {
/// <summary>Initializes a new dummy task</summary>
/// <param name="delayMilliseconds">How long the task shoudl take to execute</param>
public DummyTask(int delayMilliseconds) {
this.startGate = new ManualResetEvent(initialState: false);
this.delayMilliseconds = delayMilliseconds;
}
/// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() {
if(this.startGate != null) {
this.startGate.Dispose();
this.startGate = null;
}
}
/// <summary>Waits for the task to start (all of 100 milliseconds)</summary>
/// <returns>True if the start started, false if it didn't</returns>
public bool WaitForStart() {
return this.startGate.WaitOne(100);
}
/// <summary>Sets the task up to fail with the specified error</summary>
/// <param name="error">Error the task will fail with</param>
public void FailWith(Exception error) {
this.error = error;
}
/// <summary>Runs the task with no arguments</summary>
public void Run() {
this.startGate.Set();
++this.executionCount;
Thread.Sleep(this.delayMilliseconds);
if(this.error != null) {
throw this.error;
}
}
/// <summary>Runs the task with one argument</summary>
/// <param name="firstArgument">First argument passed from the runner</param>
public void Run(float firstArgument) {
this.startGate.Set();
++this.executionCount;
this.firstArgument = firstArgument;
Thread.Sleep(this.delayMilliseconds);
if(this.error != null) {
throw this.error;
}
}
/// <summary>Runs the task with two argument</summary>
/// <param name="firstArgument">First argument passed from the runner</param>
/// <param name="secondArgument">Second argument passed from the runner</param>
public void Run(float firstArgument, string secondArgument) {
this.startGate.Set();
++this.executionCount;
this.firstArgument = firstArgument;
this.secondArgument = secondArgument;
Thread.Sleep(this.delayMilliseconds);
if(this.error != null) {
throw this.error;
}
}
/// <summary>Runs the task with no arguments</summary>
/// <param name="cancellationToken">Token by which cancellation can be signalled</param>
public void RunCancellable(CancellationToken cancellationToken) {
this.startGate.Set();
++this.executionCount;
if(delayMilliseconds == 0) {
Thread.Sleep(0);
} else {
if(cancellationToken.WaitHandle.WaitOne(delayMilliseconds)) {
this.wasCancelled = cancellationToken.IsCancellationRequested;
cancellationToken.ThrowIfCancellationRequested();
}
}
if(this.error != null) {
throw this.error;
}
}
/// <summary>Runs the task with one argument</summary>
/// <param name="firstArgument">First argument passed from the runner</param>
/// <param name="cancellationToken">Token by which cancellation can be signalled</param>
public void RunCancellable(float firstArgument, CancellationToken cancellationToken) {
this.startGate.Set();
++this.executionCount;
this.firstArgument = firstArgument;
if(delayMilliseconds == 0) {
Thread.Sleep(0);
} else {
if(cancellationToken.WaitHandle.WaitOne(delayMilliseconds)) {
this.wasCancelled = cancellationToken.IsCancellationRequested;
cancellationToken.ThrowIfCancellationRequested();
}
}
if(this.error != null) {
throw this.error;
}
}
/// <summary>Runs the task with two argument</summary>
/// <param name="firstArgument">First argument passed from the runner</param>
/// <param name="secondArgument">Second argument passed from the runner</param>
/// <param name="cancellationToken">Token by which cancellation can be signalled</param>
public void RunCancellable(
float firstArgument, string secondArgument, CancellationToken cancellationToken
) {
this.startGate.Set();
++this.executionCount;
this.firstArgument = firstArgument;
this.secondArgument = secondArgument;
if(delayMilliseconds == 0) {
Thread.Sleep(0);
} else {
if(cancellationToken.WaitHandle.WaitOne(delayMilliseconds)) {
this.wasCancelled = cancellationToken.IsCancellationRequested;
cancellationToken.ThrowIfCancellationRequested();
}
}
if(this.error != null) {
throw this.error;
}
}
/// <summary>How many times the task was run</summary>
public int ExecutionCount {
get { return this.executionCount; }
}
/// <summary>Whether the task was cancelled by the runner itself</summary>
public bool WasCancelled {
get { return this.wasCancelled; }
}
/// <summary>What the first argument was during the last call</summary>
public float FirstArgument {
get { return this.firstArgument; }
}
/// <summary>What the second argument was during the last call</summary>
public string SecondArgument {
get { return this.secondArgument; }
}
/// <summary>Last error that was reported in the thread</summary>
private Exception error;
/// <summary>Triggered when the task has started</summary>
private ManualResetEvent startGate;
/// <summary>How long the task should take to execute in milliseconds</summary>
private int delayMilliseconds;
/// <summary>How many times the task has been executed</summary>
private volatile int executionCount;
/// <summary>Whether the task has been cancelled</summary>
private volatile bool wasCancelled;
/// <summary>First argument that was passed to the task</summary>
private volatile float firstArgument;
/// <summary>Second argument that was passed to the task</summary>
private volatile string secondArgument;
}
#endregion // class DummyRunner
/// <summary>Verifies that the thread runner has a default constructor</summary>
[Test]
public void CanBeDefaultConstructed() {
using(new DummyRunner()) { }
}
/// <summary>Checks that the runner sets and unsets its busy flag</summary>
[Test]
public void BusyFlagIsToggled() {
using(var runner = new DummyRunner()) {
int busyFlagChangeCount = runner.BusyChangeCount;
Assert.IsFalse(runner.IsBusy);
runner.RunInBackground((Action)delegate() { });
Assert.IsTrue(runner.WaitForCompletion());
Assert.GreaterOrEqual(busyFlagChangeCount + 2, runner.BusyChangeCount);
Assert.IsFalse(runner.IsBusy);
}
}
/// <summary>Lets the thread runner run a simple task in the background</summary>
[Test]
public void CanRunSimpleTaskInBackground() {
using(var task = new DummyTask(0)) {
using(var runner = new DummyRunner()) {
runner.RunInBackground(new Action(task.Run));
Assert.IsTrue(runner.WaitForCompletion());
Assert.IsNull(runner.ReportedError);
}
Assert.AreEqual(1, task.ExecutionCount);
Assert.IsFalse(task.WasCancelled);
}
}
/// <summary>
/// Checks that the thread runner is able to pass a single argument to a task
/// </summary>
[Test]
public void CanPassSingleArgumentToSimpleTask() {
using(var task = new DummyTask(0)) {
using(var runner = new DummyRunner()) {
runner.RunInBackground(new Action<float>(task.Run), 12.43f);
Assert.IsTrue(runner.WaitForCompletion());
Assert.IsNull(runner.ReportedError);
}
Assert.AreEqual(1, task.ExecutionCount);
Assert.AreEqual(12.43f, task.FirstArgument);
Assert.IsFalse(task.WasCancelled);
}
}
/// <summary>
/// Checks that the thread runner is able to pass two arguments to a task
/// </summary>
[Test]
public void CanPassTwoArgumentsToSimpleTask() {
using(var task = new DummyTask(0)) {
using(var runner = new DummyRunner()) {
runner.RunInBackground(new Action<float, string>(task.Run), 98.67f, "Hello");
Assert.IsTrue(runner.WaitForCompletion());
Assert.IsNull(runner.ReportedError);
}
Assert.AreEqual(1, task.ExecutionCount);
Assert.AreEqual(98.67f, task.FirstArgument);
Assert.AreEqual("Hello", task.SecondArgument);
Assert.IsFalse(task.WasCancelled);
}
}
/// <summary>
/// Verifies that an error happening in a simple task is reported correctly
/// </summary>
[Test]
public void SimpleTaskErrorIsReported() {
using(var task = new DummyTask(0)) {
var error = new InvalidOperationException("Mooh!");
task.FailWith(error);
using(var runner = new DummyRunner()) {
runner.RunInBackground(new Action(task.Run));
Assert.IsTrue(runner.WaitForCompletion());
Assert.AreSame(error, runner.ReportedError);
}
Assert.AreEqual(1, task.ExecutionCount);
Assert.IsFalse(task.WasCancelled);
}
}
/// <summary>Lets the thread runner run a cancellable task in the background</summary>
[Test]
public void CanRunCancellableTaskInBackground() {
using(var task = new DummyTask(100)) {
using(var runner = new DummyRunner()) {
runner.RunInBackground(new CancellableAction(task.RunCancellable));
Assert.IsTrue(task.WaitForStart());
runner.CancelAllBackgroundOperations();
Assert.IsTrue(runner.WaitForCompletion());
Assert.IsNull(runner.ReportedError);
}
Assert.AreEqual(1, task.ExecutionCount);
Assert.IsTrue(task.WasCancelled);
}
}
/// <summary>
/// Checks that the thread runner is able to pass a single argument to a task
/// that can be cancelled
/// </summary>
[Test]
public void CanPassSingleArgumentToCancellableTask() {
using(var task = new DummyTask(100)) {
using(var runner = new DummyRunner()) {
runner.RunInBackground(new CancellableAction<float>(task.RunCancellable), 12.43f);
Assert.IsTrue(task.WaitForStart());
runner.CancelAllBackgroundOperations();
Assert.IsTrue(runner.WaitForCompletion());
Assert.IsNull(runner.ReportedError);
}
Assert.AreEqual(1, task.ExecutionCount);
Assert.AreEqual(12.43f, task.FirstArgument);
Assert.IsTrue(task.WasCancelled);
}
}
/// <summary>
/// Checks that the thread runner is able to pass two arguments to a task
/// that can be cancelled
/// </summary>
[Test]
public void CanPassTwoArgumentsToCancellableTask() {
using(var task = new DummyTask(100)) {
using(var runner = new DummyRunner()) {
runner.RunInBackground(
new CancellableAction<float, string>(task.RunCancellable), 98.67f, "Hello"
);
Assert.IsTrue(task.WaitForStart());
runner.CancelAllBackgroundOperations();
Assert.IsTrue(runner.WaitForCompletion());
Assert.IsNull(runner.ReportedError);
}
Assert.AreEqual(1, task.ExecutionCount);
Assert.AreEqual(98.67f, task.FirstArgument);
Assert.AreEqual("Hello", task.SecondArgument);
Assert.IsTrue(task.WasCancelled);
}
}
}
} // namespace Nuclex.Support.Threading
#endif // UNITTEST
#endif // !NO_CONCURRENT_COLLECTIONS

View File

@ -0,0 +1,461 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if !NO_CONCURRENT_COLLECTIONS
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Nuclex.Support.Threading {
/// <summary>Executes actions in a threads</summary>
internal abstract class ThreadRunner : IDisposable {
#region interface IRunner
/// <summary>Interface for a background task runner</summary>
private interface IRunner {
/// <summary>Runs the background task</summary>
void Run();
/// <summary>The runner's cancellation token source</summary>
CancellationTokenSource CancellationTokenSource { get; }
}
#endregion // interface IRunner
#region struct Runner
/// <summary>Runs a background task with no parameters</summary>
private struct Runner : IRunner {
/// <summary>Initializes a new runner</summary>
/// <param name="action">Action the runner will execute</param>
public Runner(Action action) {
this.action = action;
}
/// <summary>Executes the runner's action</summary>
public void Run() {
this.action();
}
/// <summary>The runner's cancellation token source</summary>
public CancellationTokenSource CancellationTokenSource {
get { return null; }
}
/// <summary>Action the runner will execute</summary>
private Action action;
}
#endregion // struct Runner
#region struct CancellableRunner
/// <summary>Runs a background task with no parameters</summary>
private struct CancellableRunner : IRunner {
/// <summary>Initializes a new runner</summary>
/// <param name="action">Action the runner will execute</param>
public CancellableRunner(CancellableAction action) {
this.action = action;
this.cancellationTokenSource = new CancellationTokenSource();
}
/// <summary>Executes the runner's action</summary>
public void Run() {
this.action(this.cancellationTokenSource.Token);
}
/// <summary>The runner's cancellation token source</summary>
public CancellationTokenSource CancellationTokenSource {
get { return this.cancellationTokenSource; }
}
/// <summary>The runner's cancellation token source</summary>
private CancellationTokenSource cancellationTokenSource;
/// <summary>Action the runner will execute</summary>
private CancellableAction action;
}
#endregion // struct CancellableRunner
#region struct Runner<P1>
/// <summary>Runs a background task with one parameter</summary>
private struct Runner<P1> : IRunner {
/// <summary>Initializes a new runner</summary>
/// <param name="action">Action the runner will execute</param>
/// <param name="parameter1">Parameter that will be passed to the action</param>
public Runner(Action<P1> action, P1 parameter1) {
this.action = action;
this.parameter1 = parameter1;
}
/// <summary>Executes the runner's action</summary>
public void Run() {
this.action(this.parameter1);
}
/// <summary>The runner's cancellation token source</summary>
public CancellationTokenSource CancellationTokenSource {
get { return null; }
}
/// <summary>Action the runner will execute</summary>
private Action<P1> action;
/// <summary>Parameter that will be passed to the action</summary>
private P1 parameter1;
}
#endregion // struct Runner<P1>
#region struct CancellableRunner<P1>
/// <summary>Runs a background task with one parameter</summary>
private struct CancellableRunner<P1> : IRunner {
/// <summary>Initializes a new runner</summary>
/// <param name="action">Action the runner will execute</param>
/// <param name="parameter1">Parameter that will be passed to the action</param>
public CancellableRunner(CancellableAction<P1> action, P1 parameter1) {
this.action = action;
this.parameter1 = parameter1;
this.cancellationTokenSource = new CancellationTokenSource();
}
/// <summary>Executes the runner's action</summary>
public void Run() {
this.action(this.parameter1, this.cancellationTokenSource.Token);
}
/// <summary>The runner's cancellation token source</summary>
public CancellationTokenSource CancellationTokenSource {
get { return this.cancellationTokenSource; }
}
/// <summary>The runner's cancellation token source</summary>
private CancellationTokenSource cancellationTokenSource;
/// <summary>Action the runner will execute</summary>
private CancellableAction<P1> action;
/// <summary>Parameter that will be passed to the action</summary>
private P1 parameter1;
}
#endregion // struct CancellableRunner<P1>
#region struct Runner<P1, P2>
/// <summary>Runs a background task with one parameter</summary>
private struct Runner<P1, P2> : IRunner {
/// <summary>Initializes a new runner</summary>
/// <param name="action">Action the runner will execute</param>
/// <param name="parameter1">First parameter that will be passed to the action</param>
/// <param name="parameter2">Second parameter that will be passed to the action</param>
public Runner(Action<P1, P2> action, P1 parameter1, P2 parameter2) {
this.action = action;
this.parameter1 = parameter1;
this.parameter2 = parameter2;
}
/// <summary>Executes the runner's action</summary>
public void Run() {
this.action(this.parameter1, this.parameter2);
}
/// <summary>The runner's cancellation token source</summary>
public CancellationTokenSource CancellationTokenSource {
get { return null; }
}
/// <summary>Action the runner will execute</summary>
private Action<P1, P2> action;
/// <summary>First parameter that will be passed to the action</summary>
private P1 parameter1;
/// <summary>Second parameter that will be passed to the action</summary>
private P2 parameter2;
}
#endregion // struct Runner<P1, P2>
#region struct CancellableRunner<P1, P2>
/// <summary>Runs a background task with one parameter</summary>
private struct CancellableRunner<P1, P2> : IRunner {
/// <summary>Initializes a new runner</summary>
/// <param name="action">Action the runner will execute</param>
/// <param name="parameter1">First parameter that will be passed to the action</param>
/// <param name="parameter2">Second parameter that will be passed to the action</param>
public CancellableRunner(CancellableAction<P1, P2> action, P1 parameter1, P2 parameter2) {
this.action = action;
this.parameter1 = parameter1;
this.parameter2 = parameter2;
this.cancellationTokenSource = new CancellationTokenSource();
}
/// <summary>Executes the runner's action</summary>
public void Run() {
this.action(this.parameter1, this.parameter2, this.cancellationTokenSource.Token);
}
/// <summary>The runner's cancellation token source</summary>
public CancellationTokenSource CancellationTokenSource {
get { return this.cancellationTokenSource; }
}
/// <summary>The runner's cancellation token source</summary>
private CancellationTokenSource cancellationTokenSource;
/// <summary>Action the runner will execute</summary>
private CancellableAction<P1, P2> action;
/// <summary>First parameter that will be passed to the action</summary>
private P1 parameter1;
/// <summary>Second parameter that will be passed to the action</summary>
private P2 parameter2;
}
#endregion // struct CancellableRunner<P1, P2>
/// <summary>Initializes a new background processing handler</summary>
public ThreadRunner() {
this.executeQueuedRunnersInThreadDelegate = new Action(executeQueuedRunnersInThread);
this.queuedRunners = new ConcurrentQueue<IRunner>();
}
/// <summary>
/// Immediately cancels all operations and releases the resources used by the instance
/// </summary>
public void Dispose() {
Dispose(timeoutMilliseconds: 2500);
}
/// <summary>
/// Immediately cancels all operations and releases the resources used by the instance
/// </summary>
/// <param name="timeoutMilliseconds">
/// Time to wait for the background tasks before dropping the tasks unfinished
/// </param>
public void Dispose(int timeoutMilliseconds) {
CancelAllBackgroundOperations();
Task currentTask;
lock(this) {
currentTask = this.currentTask;
}
if(currentTask != null) {
if(!currentTask.Wait(timeoutMilliseconds)) {
Debug.Assert(false, "Task does not support cancellation or did not cancel in time");
}
lock(this) {
this.currentTask = null;
IsBusy = false;
}
}
}
/// <summary>Whether the view model is currently busy executing a task</summary>
public bool IsBusy {
get { return this.isBusy; }
private set {
if(value != this.isBusy) {
this.isBusy = value;
BusyChanged();
}
}
}
/// <summary>Reports an error</summary>
/// <param name="exception">Error that will be reported</param>
protected abstract void ReportError(Exception exception);
/// <summary>Called when the status of the busy flag changes</summary>
protected abstract void BusyChanged();
/// <summary>Executes the specified operation in the background</summary>
/// <param name="action">Action that will be executed in the background</param>
public void RunInBackground(Action action) {
this.queuedRunners.Enqueue(new Runner(action));
startBackgroundProcessingIfNecessary();
}
/// <summary>Executes the specified operation in the background</summary>
/// <param name="action">Action that will be executed in the background</param>
public void RunInBackground(CancellableAction action) {
this.queuedRunners.Enqueue(new CancellableRunner(action));
startBackgroundProcessingIfNecessary();
}
/// <summary>Executes the specified operation in the background</summary>
/// <param name="action">Action that will be executed in the background</param>
/// <param name="parameter1">Parameter that will be passed to the action</param>
public void RunInBackground<P1>(Action<P1> action, P1 parameter1) {
this.queuedRunners.Enqueue(new Runner<P1>(action, parameter1));
startBackgroundProcessingIfNecessary();
}
/// <summary>Executes the specified operation in the background</summary>
/// <param name="action">Action that will be executed in the background</param>
/// <param name="parameter1">Parameter that will be passed to the action</param>
public void RunInBackground<P1>(CancellableAction<P1> action, P1 parameter1) {
this.queuedRunners.Enqueue(new CancellableRunner<P1>(action, parameter1));
startBackgroundProcessingIfNecessary();
}
/// <summary>Executes the specified operation in the background</summary>
/// <param name="action">Action that will be executed in the background</param>
/// <param name="parameter1">First parameter that will be passed to the action</param>
/// <param name="parameter2">Second parameter that will be passed to the action</param>
public void RunInBackground<P1, P2>(Action<P1, P2> action, P1 parameter1, P2 parameter2) {
this.queuedRunners.Enqueue(new Runner<P1, P2>(action, parameter1, parameter2));
startBackgroundProcessingIfNecessary();
}
/// <summary>Executes the specified operation in the background</summary>
/// <param name="action">Action that will be executed in the background</param>
/// <param name="parameter1">First parameter that will be passed to the action</param>
/// <param name="parameter2">Second parameter that will be passed to the action</param>
public void RunInBackground<P1, P2>(
CancellableAction<P1, P2> action, P1 parameter1, P2 parameter2
) {
this.queuedRunners.Enqueue(new CancellableRunner<P1, P2>(action, parameter1, parameter2));
startBackgroundProcessingIfNecessary();
}
/// <summary>Cancels the currently running background operation</summary>
public void CancelBackgroundOperation() {
IRunner currentRunner = this.currentRunner;
if(currentRunner != null) {
CancellationTokenSource cancellationTokenSource = currentRunner.CancellationTokenSource;
if(cancellationTokenSource != null) {
cancellationTokenSource.Cancel();
}
}
}
/// <summary>Cancels all queued and the currently running background operation</summary>
public void CancelAllBackgroundOperations() {
IRunner runner;
while(this.queuedRunners.TryDequeue(out runner)) {
CancellationTokenSource cancellationTokenSource = runner.CancellationTokenSource;
if(cancellationTokenSource != null) {
cancellationTokenSource.Cancel();
}
}
CancelBackgroundOperation();
}
/// <summary>Whether the background operation has been cancelled</summary>
//[Obsolete("Please use a method accepting a cancellation token instead of using this")]
public bool IsBackgroundOperationCancelled {
get {
IRunner currentRunner = this.currentRunner;
if(currentRunner != null) {
return currentRunner.CancellationTokenSource.IsCancellationRequested;
} else {
return false;
}
}
}
/// <summary>Throws an exception if the background operation was cancelled</summary>
//[Obsolete("Please use a method accepting a cancellation token instead of using this")]
public void ThrowIfBackgroundOperationCancelled() {
IRunner currentRunner = this.currentRunner;
if(currentRunner != null) {
currentRunner.CancellationTokenSource.Token.ThrowIfCancellationRequested();
}
}
/// <summary>Executes the queued runners in the background</summary>
private void executeQueuedRunnersInThread() {
IsBusy = true;
IRunner runner;
while(this.queuedRunners.TryDequeue(out runner)) {
try {
this.currentRunner = runner;
runner.Run();
}
catch(OperationCanceledException) {
// Ignore
}
catch(Exception exception) {
this.currentRunner = null; // When the error is reported this should already be null
ReportError(exception);
}
this.currentRunner = null;
}
lock(this) {
this.currentTask = null;
IsBusy = false;
}
}
/// <summary>Starts the background processing thread, if needed</summary>
private void startBackgroundProcessingIfNecessary() {
Task currentTask;
lock(this) {
if(this.currentTask == null) {
currentTask = new Task(this.executeQueuedRunnersInThreadDelegate);
this.currentTask = currentTask;
} else {
return; // Task is already running
}
}
// Start the task outside of the lock statement so that when the thread starts to run,
// it is guaranteed to read the currentTask variable as the task we just created.
currentTask.Start();
}
/// <summary>Whether the view model is currently busy executing a task</summary>
private volatile bool isBusy;
/// <summary>Delegate for the executedQueuedRunnersInThread() method</summary>
private Action executeQueuedRunnersInThreadDelegate;
/// <summary>Queued background operations</summary>
private ConcurrentQueue<IRunner> queuedRunners;
/// <summary>Runner currently executing in the background</summary>
private volatile IRunner currentRunner;
/// <summary>Task that is currently executing the runners</summary>
private Task currentTask;
}
} // namespace Nuclex.Support.Threading
#endif // !NO_CONCURRENT_COLLECTIONS