Cleaned and added a base class for view models with background processing tha uses the new ThreadedAction class to do its background processing; added a view binding interface for WinForms that emulates the WPF design
git-svn-id: file:///srv/devel/repo-conversion/nusu@334 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
97183d2335
commit
8cc16143de
|
@ -28,433 +28,433 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Nuclex.Support.Threading {
|
namespace Nuclex.Support.Threading {
|
||||||
|
|
||||||
/// <summary>Executes actions in a threads</summary>
|
/// <summary>Executes actions in a threads</summary>
|
||||||
public abstract class ThreadRunner : IDisposable {
|
public abstract class ThreadRunner : IDisposable {
|
||||||
|
|
||||||
#region interface IRunner
|
#region interface IRunner
|
||||||
|
|
||||||
/// <summary>Interface for a background task runner</summary>
|
/// <summary>Interface for a background task runner</summary>
|
||||||
private interface IRunner {
|
private interface IRunner {
|
||||||
|
|
||||||
/// <summary>Runs the background task</summary>
|
/// <summary>Runs the background task</summary>
|
||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
CancellationTokenSource CancellationTokenSource { get; }
|
CancellationTokenSource CancellationTokenSource { get; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // interface IRunner
|
#endregion // interface IRunner
|
||||||
|
|
||||||
#region struct Runner
|
#region struct Runner
|
||||||
|
|
||||||
/// <summary>Runs a background task with no parameters</summary>
|
/// <summary>Runs a background task with no parameters</summary>
|
||||||
private struct Runner : IRunner {
|
private struct Runner : IRunner {
|
||||||
|
|
||||||
/// <summary>Initializes a new runner</summary>
|
/// <summary>Initializes a new runner</summary>
|
||||||
/// <param name="action">Action the runner will execute</param>
|
/// <param name="action">Action the runner will execute</param>
|
||||||
public Runner(Action action) {
|
public Runner(Action action) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the runner's action</summary>
|
/// <summary>Executes the runner's action</summary>
|
||||||
public void Run() {
|
public void Run() {
|
||||||
this.action();
|
this.action();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
public CancellationTokenSource CancellationTokenSource {
|
public CancellationTokenSource CancellationTokenSource {
|
||||||
get { return null; }
|
get { return null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Action the runner will execute</summary>
|
/// <summary>Action the runner will execute</summary>
|
||||||
private Action action;
|
private Action action;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // struct Runner
|
#endregion // struct Runner
|
||||||
|
|
||||||
#region struct CancellableRunner
|
#region struct CancellableRunner
|
||||||
|
|
||||||
/// <summary>Runs a background task with no parameters</summary>
|
/// <summary>Runs a background task with no parameters</summary>
|
||||||
private struct CancellableRunner : IRunner {
|
private struct CancellableRunner : IRunner {
|
||||||
|
|
||||||
/// <summary>Initializes a new runner</summary>
|
/// <summary>Initializes a new runner</summary>
|
||||||
/// <param name="action">Action the runner will execute</param>
|
/// <param name="action">Action the runner will execute</param>
|
||||||
public CancellableRunner(CancellableAction action) {
|
public CancellableRunner(CancellableAction action) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.cancellationTokenSource = new CancellationTokenSource();
|
this.cancellationTokenSource = new CancellationTokenSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the runner's action</summary>
|
/// <summary>Executes the runner's action</summary>
|
||||||
public void Run() {
|
public void Run() {
|
||||||
this.action(this.cancellationTokenSource.Token);
|
this.action(this.cancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
public CancellationTokenSource CancellationTokenSource {
|
public CancellationTokenSource CancellationTokenSource {
|
||||||
get { return this.cancellationTokenSource; }
|
get { return this.cancellationTokenSource; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
private CancellationTokenSource cancellationTokenSource;
|
private CancellationTokenSource cancellationTokenSource;
|
||||||
/// <summary>Action the runner will execute</summary>
|
/// <summary>Action the runner will execute</summary>
|
||||||
private CancellableAction action;
|
private CancellableAction action;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // struct CancellableRunner
|
#endregion // struct CancellableRunner
|
||||||
|
|
||||||
#region struct Runner<P1>
|
#region struct Runner<P1>
|
||||||
|
|
||||||
/// <summary>Runs a background task with one parameter</summary>
|
/// <summary>Runs a background task with one parameter</summary>
|
||||||
private struct Runner<P1> : IRunner {
|
private struct Runner<P1> : IRunner {
|
||||||
|
|
||||||
/// <summary>Initializes a new runner</summary>
|
/// <summary>Initializes a new runner</summary>
|
||||||
/// <param name="action">Action the runner will execute</param>
|
/// <param name="action">Action the runner will execute</param>
|
||||||
/// <param name="parameter1">Parameter that will be passed to the action</param>
|
/// <param name="parameter1">Parameter that will be passed to the action</param>
|
||||||
public Runner(Action<P1> action, P1 parameter1) {
|
public Runner(Action<P1> action, P1 parameter1) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.parameter1 = parameter1;
|
this.parameter1 = parameter1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the runner's action</summary>
|
/// <summary>Executes the runner's action</summary>
|
||||||
public void Run() {
|
public void Run() {
|
||||||
this.action(this.parameter1);
|
this.action(this.parameter1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
public CancellationTokenSource CancellationTokenSource {
|
public CancellationTokenSource CancellationTokenSource {
|
||||||
get { return null; }
|
get { return null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Action the runner will execute</summary>
|
/// <summary>Action the runner will execute</summary>
|
||||||
private Action<P1> action;
|
private Action<P1> action;
|
||||||
/// <summary>Parameter that will be passed to the action</summary>
|
/// <summary>Parameter that will be passed to the action</summary>
|
||||||
private P1 parameter1;
|
private P1 parameter1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // struct Runner<P1>
|
#endregion // struct Runner<P1>
|
||||||
|
|
||||||
#region struct CancellableRunner<P1>
|
#region struct CancellableRunner<P1>
|
||||||
|
|
||||||
/// <summary>Runs a background task with one parameter</summary>
|
/// <summary>Runs a background task with one parameter</summary>
|
||||||
private struct CancellableRunner<P1> : IRunner {
|
private struct CancellableRunner<P1> : IRunner {
|
||||||
|
|
||||||
/// <summary>Initializes a new runner</summary>
|
/// <summary>Initializes a new runner</summary>
|
||||||
/// <param name="action">Action the runner will execute</param>
|
/// <param name="action">Action the runner will execute</param>
|
||||||
/// <param name="parameter1">Parameter that will be passed to the action</param>
|
/// <param name="parameter1">Parameter that will be passed to the action</param>
|
||||||
public CancellableRunner(CancellableAction<P1> action, P1 parameter1) {
|
public CancellableRunner(CancellableAction<P1> action, P1 parameter1) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.parameter1 = parameter1;
|
this.parameter1 = parameter1;
|
||||||
this.cancellationTokenSource = new CancellationTokenSource();
|
this.cancellationTokenSource = new CancellationTokenSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the runner's action</summary>
|
/// <summary>Executes the runner's action</summary>
|
||||||
public void Run() {
|
public void Run() {
|
||||||
this.action(this.parameter1, this.cancellationTokenSource.Token);
|
this.action(this.parameter1, this.cancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
public CancellationTokenSource CancellationTokenSource {
|
public CancellationTokenSource CancellationTokenSource {
|
||||||
get { return this.cancellationTokenSource; }
|
get { return this.cancellationTokenSource; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
private CancellationTokenSource cancellationTokenSource;
|
private CancellationTokenSource cancellationTokenSource;
|
||||||
/// <summary>Action the runner will execute</summary>
|
/// <summary>Action the runner will execute</summary>
|
||||||
private CancellableAction<P1> action;
|
private CancellableAction<P1> action;
|
||||||
/// <summary>Parameter that will be passed to the action</summary>
|
/// <summary>Parameter that will be passed to the action</summary>
|
||||||
private P1 parameter1;
|
private P1 parameter1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // struct CancellableRunner<P1>
|
#endregion // struct CancellableRunner<P1>
|
||||||
|
|
||||||
#region struct Runner<P1, P2>
|
#region struct Runner<P1, P2>
|
||||||
|
|
||||||
/// <summary>Runs a background task with one parameter</summary>
|
/// <summary>Runs a background task with one parameter</summary>
|
||||||
private struct Runner<P1, P2> : IRunner {
|
private struct Runner<P1, P2> : IRunner {
|
||||||
|
|
||||||
/// <summary>Initializes a new runner</summary>
|
/// <summary>Initializes a new runner</summary>
|
||||||
/// <param name="action">Action the runner will execute</param>
|
/// <param name="action">Action the runner will execute</param>
|
||||||
/// <param name="parameter1">First parameter that will be passed to the action</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>
|
/// <param name="parameter2">Second parameter that will be passed to the action</param>
|
||||||
public Runner(Action<P1, P2> action, P1 parameter1, P2 parameter2) {
|
public Runner(Action<P1, P2> action, P1 parameter1, P2 parameter2) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.parameter1 = parameter1;
|
this.parameter1 = parameter1;
|
||||||
this.parameter2 = parameter2;
|
this.parameter2 = parameter2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the runner's action</summary>
|
/// <summary>Executes the runner's action</summary>
|
||||||
public void Run() {
|
public void Run() {
|
||||||
this.action(this.parameter1, this.parameter2);
|
this.action(this.parameter1, this.parameter2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
public CancellationTokenSource CancellationTokenSource {
|
public CancellationTokenSource CancellationTokenSource {
|
||||||
get { return null; }
|
get { return null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Action the runner will execute</summary>
|
/// <summary>Action the runner will execute</summary>
|
||||||
private Action<P1, P2> action;
|
private Action<P1, P2> action;
|
||||||
/// <summary>First parameter that will be passed to the action</summary>
|
/// <summary>First parameter that will be passed to the action</summary>
|
||||||
private P1 parameter1;
|
private P1 parameter1;
|
||||||
/// <summary>Second parameter that will be passed to the action</summary>
|
/// <summary>Second parameter that will be passed to the action</summary>
|
||||||
private P2 parameter2;
|
private P2 parameter2;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // struct Runner<P1, P2>
|
#endregion // struct Runner<P1, P2>
|
||||||
|
|
||||||
#region struct CancellableRunner<P1, P2>
|
#region struct CancellableRunner<P1, P2>
|
||||||
|
|
||||||
/// <summary>Runs a background task with one parameter</summary>
|
/// <summary>Runs a background task with one parameter</summary>
|
||||||
private struct CancellableRunner<P1, P2> : IRunner {
|
private struct CancellableRunner<P1, P2> : IRunner {
|
||||||
|
|
||||||
/// <summary>Initializes a new runner</summary>
|
/// <summary>Initializes a new runner</summary>
|
||||||
/// <param name="action">Action the runner will execute</param>
|
/// <param name="action">Action the runner will execute</param>
|
||||||
/// <param name="parameter1">First parameter that will be passed to the action</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>
|
/// <param name="parameter2">Second parameter that will be passed to the action</param>
|
||||||
public CancellableRunner(CancellableAction<P1, P2> action, P1 parameter1, P2 parameter2) {
|
public CancellableRunner(CancellableAction<P1, P2> action, P1 parameter1, P2 parameter2) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.parameter1 = parameter1;
|
this.parameter1 = parameter1;
|
||||||
this.parameter2 = parameter2;
|
this.parameter2 = parameter2;
|
||||||
this.cancellationTokenSource = new CancellationTokenSource();
|
this.cancellationTokenSource = new CancellationTokenSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the runner's action</summary>
|
/// <summary>Executes the runner's action</summary>
|
||||||
public void Run() {
|
public void Run() {
|
||||||
this.action(this.parameter1, this.parameter2, this.cancellationTokenSource.Token);
|
this.action(this.parameter1, this.parameter2, this.cancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
public CancellationTokenSource CancellationTokenSource {
|
public CancellationTokenSource CancellationTokenSource {
|
||||||
get { return this.cancellationTokenSource; }
|
get { return this.cancellationTokenSource; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The runner's cancellation token source</summary>
|
/// <summary>The runner's cancellation token source</summary>
|
||||||
private CancellationTokenSource cancellationTokenSource;
|
private CancellationTokenSource cancellationTokenSource;
|
||||||
/// <summary>Action the runner will execute</summary>
|
/// <summary>Action the runner will execute</summary>
|
||||||
private CancellableAction<P1, P2> action;
|
private CancellableAction<P1, P2> action;
|
||||||
/// <summary>First parameter that will be passed to the action</summary>
|
/// <summary>First parameter that will be passed to the action</summary>
|
||||||
private P1 parameter1;
|
private P1 parameter1;
|
||||||
/// <summary>Second parameter that will be passed to the action</summary>
|
/// <summary>Second parameter that will be passed to the action</summary>
|
||||||
private P2 parameter2;
|
private P2 parameter2;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // struct CancellableRunner<P1, P2>
|
#endregion // struct CancellableRunner<P1, P2>
|
||||||
|
|
||||||
/// <summary>Initializes a new background processing handler</summary>
|
/// <summary>Initializes a new background processing handler</summary>
|
||||||
public ThreadRunner() {
|
public ThreadRunner() {
|
||||||
this.executeQueuedRunnersInThreadDelegate = new Action(executeQueuedRunnersInThread);
|
this.executeQueuedRunnersInThreadDelegate = new Action(executeQueuedRunnersInThread);
|
||||||
this.queuedRunners = new ConcurrentQueue<IRunner>();
|
this.queuedRunners = new ConcurrentQueue<IRunner>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Immediately cancels all operations and releases the resources used by the instance
|
/// Immediately cancels all operations and releases the resources used by the instance
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
Dispose(timeoutMilliseconds: 2500);
|
Dispose(timeoutMilliseconds: 2500);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Immediately cancels all operations and releases the resources used by the instance
|
/// Immediately cancels all operations and releases the resources used by the instance
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="timeoutMilliseconds">
|
/// <param name="timeoutMilliseconds">
|
||||||
/// Time to wait for the background tasks before dropping the tasks unfinished
|
/// Time to wait for the background tasks before dropping the tasks unfinished
|
||||||
/// </param>
|
/// </param>
|
||||||
public void Dispose(int timeoutMilliseconds) {
|
public void Dispose(int timeoutMilliseconds) {
|
||||||
CancelAllBackgroundOperations();
|
CancelAllBackgroundOperations();
|
||||||
|
|
||||||
Task currentTask;
|
Task currentTask;
|
||||||
lock(this) {
|
lock(this) {
|
||||||
currentTask = this.currentTask;
|
currentTask = this.currentTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(currentTask != null) {
|
if(currentTask != null) {
|
||||||
if(!currentTask.Wait(timeoutMilliseconds)) {
|
if(!currentTask.Wait(timeoutMilliseconds)) {
|
||||||
Debug.Assert(false, "Task does not support cancellation or did not cancel in time");
|
Debug.Assert(false, "Task does not support cancellation or did not cancel in time");
|
||||||
}
|
}
|
||||||
lock(this) {
|
lock(this) {
|
||||||
this.currentTask = null;
|
this.currentTask = null;
|
||||||
IsBusy = false;
|
IsBusy = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Whether the view model is currently busy executing a task</summary>
|
/// <summary>Whether the view model is currently busy executing a task</summary>
|
||||||
public bool IsBusy {
|
public bool IsBusy {
|
||||||
get { return this.isBusy; }
|
get { return this.isBusy; }
|
||||||
private set {
|
private set {
|
||||||
if(value != this.isBusy) {
|
if(value != this.isBusy) {
|
||||||
this.isBusy = value;
|
this.isBusy = value;
|
||||||
BusyChanged();
|
BusyChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Reports an error</summary>
|
/// <summary>Reports an error</summary>
|
||||||
/// <param name="exception">Error that will be reported</param>
|
/// <param name="exception">Error that will be reported</param>
|
||||||
protected abstract void ReportError(Exception exception);
|
protected abstract void ReportError(Exception exception);
|
||||||
|
|
||||||
/// <summary>Called when the status of the busy flag changes</summary>
|
/// <summary>Called when the status of the busy flag changes</summary>
|
||||||
protected abstract void BusyChanged();
|
protected abstract void BusyChanged();
|
||||||
|
|
||||||
/// <summary>Executes the specified operation in the background</summary>
|
/// <summary>Executes the specified operation in the background</summary>
|
||||||
/// <param name="action">Action that will be executed in the background</param>
|
/// <param name="action">Action that will be executed in the background</param>
|
||||||
public void RunInBackground(Action action) {
|
public void RunInBackground(Action action) {
|
||||||
this.queuedRunners.Enqueue(new Runner(action));
|
this.queuedRunners.Enqueue(new Runner(action));
|
||||||
startBackgroundProcessingIfNecessary();
|
startBackgroundProcessingIfNecessary();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the specified operation in the background</summary>
|
/// <summary>Executes the specified operation in the background</summary>
|
||||||
/// <param name="action">Action that will be executed in the background</param>
|
/// <param name="action">Action that will be executed in the background</param>
|
||||||
public void RunInBackground(CancellableAction action) {
|
public void RunInBackground(CancellableAction action) {
|
||||||
this.queuedRunners.Enqueue(new CancellableRunner(action));
|
this.queuedRunners.Enqueue(new CancellableRunner(action));
|
||||||
startBackgroundProcessingIfNecessary();
|
startBackgroundProcessingIfNecessary();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the specified operation in the background</summary>
|
/// <summary>Executes the specified operation in the background</summary>
|
||||||
/// <param name="action">Action that will be executed in the background</param>
|
/// <param name="action">Action that will be executed in the background</param>
|
||||||
/// <param name="parameter1">Parameter that will be passed to the action</param>
|
/// <param name="parameter1">Parameter that will be passed to the action</param>
|
||||||
public void RunInBackground<P1>(Action<P1> action, P1 parameter1) {
|
public void RunInBackground<P1>(Action<P1> action, P1 parameter1) {
|
||||||
this.queuedRunners.Enqueue(new Runner<P1>(action, parameter1));
|
this.queuedRunners.Enqueue(new Runner<P1>(action, parameter1));
|
||||||
startBackgroundProcessingIfNecessary();
|
startBackgroundProcessingIfNecessary();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the specified operation in the background</summary>
|
/// <summary>Executes the specified operation in the background</summary>
|
||||||
/// <param name="action">Action that will be executed in the background</param>
|
/// <param name="action">Action that will be executed in the background</param>
|
||||||
/// <param name="parameter1">Parameter that will be passed to the action</param>
|
/// <param name="parameter1">Parameter that will be passed to the action</param>
|
||||||
public void RunInBackground<P1>(CancellableAction<P1> action, P1 parameter1) {
|
public void RunInBackground<P1>(CancellableAction<P1> action, P1 parameter1) {
|
||||||
this.queuedRunners.Enqueue(new CancellableRunner<P1>(action, parameter1));
|
this.queuedRunners.Enqueue(new CancellableRunner<P1>(action, parameter1));
|
||||||
startBackgroundProcessingIfNecessary();
|
startBackgroundProcessingIfNecessary();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the specified operation in the background</summary>
|
/// <summary>Executes the specified operation in the background</summary>
|
||||||
/// <param name="action">Action that will be executed in the background</param>
|
/// <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="parameter1">First parameter that will be passed to the action</param>
|
||||||
/// <param name="parameter2">Second 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) {
|
public void RunInBackground<P1, P2>(Action<P1, P2> action, P1 parameter1, P2 parameter2) {
|
||||||
this.queuedRunners.Enqueue(new Runner<P1, P2>(action, parameter1, parameter2));
|
this.queuedRunners.Enqueue(new Runner<P1, P2>(action, parameter1, parameter2));
|
||||||
startBackgroundProcessingIfNecessary();
|
startBackgroundProcessingIfNecessary();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the specified operation in the background</summary>
|
/// <summary>Executes the specified operation in the background</summary>
|
||||||
/// <param name="action">Action that will be executed in the background</param>
|
/// <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="parameter1">First parameter that will be passed to the action</param>
|
||||||
/// <param name="parameter2">Second 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>(
|
public void RunInBackground<P1, P2>(
|
||||||
CancellableAction<P1, P2> action, P1 parameter1, P2 parameter2
|
CancellableAction<P1, P2> action, P1 parameter1, P2 parameter2
|
||||||
) {
|
) {
|
||||||
this.queuedRunners.Enqueue(new CancellableRunner<P1, P2>(action, parameter1, parameter2));
|
this.queuedRunners.Enqueue(new CancellableRunner<P1, P2>(action, parameter1, parameter2));
|
||||||
startBackgroundProcessingIfNecessary();
|
startBackgroundProcessingIfNecessary();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Cancels the currently running background operation</summary>
|
/// <summary>Cancels the currently running background operation</summary>
|
||||||
public void CancelBackgroundOperation() {
|
public void CancelBackgroundOperation() {
|
||||||
IRunner currentRunner = this.currentRunner;
|
IRunner currentRunner = this.currentRunner;
|
||||||
if(currentRunner != null) {
|
if(currentRunner != null) {
|
||||||
CancellationTokenSource cancellationTokenSource = currentRunner.CancellationTokenSource;
|
CancellationTokenSource cancellationTokenSource = currentRunner.CancellationTokenSource;
|
||||||
if(cancellationTokenSource != null) {
|
if(cancellationTokenSource != null) {
|
||||||
cancellationTokenSource.Cancel();
|
cancellationTokenSource.Cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Cancels all queued and the currently running background operation</summary>
|
/// <summary>Cancels all queued and the currently running background operation</summary>
|
||||||
public void CancelAllBackgroundOperations() {
|
public void CancelAllBackgroundOperations() {
|
||||||
IRunner runner;
|
IRunner runner;
|
||||||
while(this.queuedRunners.TryDequeue(out runner)) {
|
while(this.queuedRunners.TryDequeue(out runner)) {
|
||||||
CancellationTokenSource cancellationTokenSource = runner.CancellationTokenSource;
|
CancellationTokenSource cancellationTokenSource = runner.CancellationTokenSource;
|
||||||
if(cancellationTokenSource != null) {
|
if(cancellationTokenSource != null) {
|
||||||
cancellationTokenSource.Cancel();
|
cancellationTokenSource.Cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CancelBackgroundOperation();
|
CancelBackgroundOperation();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Whether the background operation has been cancelled</summary>
|
/// <summary>Whether the background operation has been cancelled</summary>
|
||||||
//[Obsolete("Please use a method accepting a cancellation token instead of using this")]
|
//[Obsolete("Please use a method accepting a cancellation token instead of using this")]
|
||||||
public bool IsBackgroundOperationCancelled {
|
public bool IsBackgroundOperationCancelled {
|
||||||
get {
|
get {
|
||||||
IRunner currentRunner = this.currentRunner;
|
IRunner currentRunner = this.currentRunner;
|
||||||
if(currentRunner != null) {
|
if(currentRunner != null) {
|
||||||
return currentRunner.CancellationTokenSource.IsCancellationRequested;
|
return currentRunner.CancellationTokenSource.IsCancellationRequested;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Throws an exception if the background operation was cancelled</summary>
|
/// <summary>Throws an exception if the background operation was cancelled</summary>
|
||||||
//[Obsolete("Please use a method accepting a cancellation token instead of using this")]
|
//[Obsolete("Please use a method accepting a cancellation token instead of using this")]
|
||||||
public void ThrowIfBackgroundOperationCancelled() {
|
public void ThrowIfBackgroundOperationCancelled() {
|
||||||
IRunner currentRunner = this.currentRunner;
|
IRunner currentRunner = this.currentRunner;
|
||||||
if(currentRunner != null) {
|
if(currentRunner != null) {
|
||||||
currentRunner.CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
currentRunner.CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Executes the queued runners in the background</summary>
|
/// <summary>Executes the queued runners in the background</summary>
|
||||||
private void executeQueuedRunnersInThread() {
|
private void executeQueuedRunnersInThread() {
|
||||||
IsBusy = true;
|
IsBusy = true;
|
||||||
|
|
||||||
IRunner runner;
|
IRunner runner;
|
||||||
while(this.queuedRunners.TryDequeue(out runner)) {
|
while(this.queuedRunners.TryDequeue(out runner)) {
|
||||||
try {
|
try {
|
||||||
this.currentRunner = runner;
|
this.currentRunner = runner;
|
||||||
runner.Run();
|
runner.Run();
|
||||||
}
|
}
|
||||||
catch(OperationCanceledException) {
|
catch(OperationCanceledException) {
|
||||||
// Ignore
|
// Ignore
|
||||||
}
|
}
|
||||||
catch(Exception exception) {
|
catch(Exception exception) {
|
||||||
this.currentRunner = null; // When the error is reported this should already be null
|
this.currentRunner = null; // When the error is reported this should already be null
|
||||||
ReportError(exception);
|
ReportError(exception);
|
||||||
}
|
}
|
||||||
this.currentRunner = null;
|
this.currentRunner = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
lock(this) {
|
lock(this) {
|
||||||
this.currentTask = null;
|
this.currentTask = null;
|
||||||
IsBusy = false;
|
IsBusy = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Starts the background processing thread, if needed</summary>
|
/// <summary>Starts the background processing thread, if needed</summary>
|
||||||
private void startBackgroundProcessingIfNecessary() {
|
private void startBackgroundProcessingIfNecessary() {
|
||||||
Task currentTask;
|
Task currentTask;
|
||||||
|
|
||||||
lock(this) {
|
lock(this) {
|
||||||
if(this.currentTask == null) {
|
if(this.currentTask == null) {
|
||||||
currentTask = new Task(this.executeQueuedRunnersInThreadDelegate);
|
currentTask = new Task(this.executeQueuedRunnersInThreadDelegate);
|
||||||
this.currentTask = currentTask;
|
this.currentTask = currentTask;
|
||||||
} else {
|
} else {
|
||||||
return; // Task is already running
|
return; // Task is already running
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the task outside of the lock statement so that when the thread starts to run,
|
// 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.
|
// it is guaranteed to read the currentTask variable as the task we just created.
|
||||||
currentTask.Start();
|
currentTask.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Whether the view model is currently busy executing a task</summary>
|
/// <summary>Whether the view model is currently busy executing a task</summary>
|
||||||
private volatile bool isBusy;
|
private volatile bool isBusy;
|
||||||
/// <summary>Delegate for the executedQueuedRunnersInThread() method</summary>
|
/// <summary>Delegate for the executedQueuedRunnersInThread() method</summary>
|
||||||
private Action executeQueuedRunnersInThreadDelegate;
|
private Action executeQueuedRunnersInThreadDelegate;
|
||||||
/// <summary>Queued background operations</summary>
|
/// <summary>Queued background operations</summary>
|
||||||
private ConcurrentQueue<IRunner> queuedRunners;
|
private ConcurrentQueue<IRunner> queuedRunners;
|
||||||
/// <summary>Runner currently executing in the background</summary>
|
/// <summary>Runner currently executing in the background</summary>
|
||||||
private volatile IRunner currentRunner;
|
private volatile IRunner currentRunner;
|
||||||
/// <summary>Task that is currently executing the runners</summary>
|
/// <summary>Task that is currently executing the runners</summary>
|
||||||
private Task currentTask;
|
private Task currentTask;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Nuclex.Support.Threading
|
} // namespace Nuclex.Support.Threading
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user