2007-05-16 20:28:23 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2008-01-07 18:04:02 +00:00
|
|
|
Copyright (C) 2002-2008 Nuclex Development Labs
|
2007-05-16 20:28:23 +00:00
|
|
|
|
|
|
|
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
|
2007-07-24 20:15:19 +00:00
|
|
|
|
2007-04-16 17:18:16 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2007-07-04 19:19:48 +00:00
|
|
|
using System.Threading;
|
2007-04-16 17:18:16 +00:00
|
|
|
|
2007-07-01 19:27:40 +00:00
|
|
|
namespace Nuclex.Support.Scheduling {
|
2007-07-04 19:19:48 +00:00
|
|
|
|
|
|
|
/// <summary>Operation that executes a method in a background thread</summary>
|
2007-07-05 20:02:02 +00:00
|
|
|
public abstract class ThreadOperation : Operation {
|
2007-07-04 19:19:48 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2007-08-31 21:24:40 +00:00
|
|
|
/// Initializes a new threaded operation.
|
2007-07-04 19:19:48 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2007-08-31 21:24:40 +00:00
|
|
|
/// Uses a ThreadPool thread to execute the method in a background thread.
|
2007-07-04 19:19:48 +00:00
|
|
|
/// </remarks>
|
2007-07-05 20:02:02 +00:00
|
|
|
public ThreadOperation() : this(true) { }
|
2007-07-04 19:19:48 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2007-08-31 21:24:40 +00:00
|
|
|
/// Initializes a new threaded operation which optionally uses the ThreadPool.
|
2007-07-04 19:19:48 +00:00
|
|
|
/// </summary>
|
2007-08-31 21:24:40 +00:00
|
|
|
/// <param name="useThreadPool">Whether to use a ThreadPool thread.</param>
|
2007-07-04 19:19:48 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// If useThreadPool is false, a new thread will be created. This guarantees
|
|
|
|
/// that the method will be executed immediately but has an impact on
|
|
|
|
/// performance since the creation of new threads is not a cheap operation.
|
|
|
|
/// </remarks>
|
2007-07-05 20:02:02 +00:00
|
|
|
public ThreadOperation(bool useThreadPool) {
|
|
|
|
this.useThreadPool = useThreadPool;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Launches the background operation</summary>
|
2008-02-07 21:25:34 +00:00
|
|
|
public override void Start() {
|
2007-07-04 19:19:48 +00:00
|
|
|
if(useThreadPool) {
|
2007-07-05 20:02:02 +00:00
|
|
|
ThreadPool.QueueUserWorkItem(callMethod);
|
2007-07-04 19:19:48 +00:00
|
|
|
} else {
|
|
|
|
Thread thread = new Thread(callMethod);
|
2007-07-05 20:02:02 +00:00
|
|
|
thread.Name = "Nuclex.Support.Scheduling.ThreadOperation";
|
2007-07-04 19:19:48 +00:00
|
|
|
thread.IsBackground = true;
|
2007-07-05 20:02:02 +00:00
|
|
|
thread.Start();
|
2007-07-04 19:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-05 20:02:02 +00:00
|
|
|
/// <summary>Contains the payload to be executed in the background thread</summary>
|
|
|
|
protected abstract void Execute();
|
|
|
|
|
2007-07-04 19:19:48 +00:00
|
|
|
/// <summary>Invokes the delegate passed as an argument</summary>
|
2007-07-05 20:02:02 +00:00
|
|
|
/// <param name="state">Not used</param>
|
|
|
|
private void callMethod(object state) {
|
2007-07-04 19:19:48 +00:00
|
|
|
try {
|
2007-07-05 20:02:02 +00:00
|
|
|
Execute();
|
2007-07-04 19:19:48 +00:00
|
|
|
}
|
|
|
|
catch(Exception exception) {
|
2008-03-26 20:20:38 +00:00
|
|
|
this.exception = exception;
|
2007-07-04 19:19:48 +00:00
|
|
|
}
|
|
|
|
finally {
|
2007-07-05 20:02:02 +00:00
|
|
|
OnAsyncEnded();
|
2007-07-04 19:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-26 20:20:38 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Allows the specific request implementation to re-throw an exception if
|
|
|
|
/// the background process finished unsuccessfully
|
|
|
|
/// </summary>
|
|
|
|
protected override void ReraiseExceptions() {
|
|
|
|
if(this.exception != null)
|
|
|
|
throw this.exception;
|
|
|
|
}
|
|
|
|
|
2007-07-05 20:02:02 +00:00
|
|
|
/// <summary>Whether to use the ThreadPool for obtaining a background thread</summary>
|
|
|
|
private bool useThreadPool;
|
2008-03-26 20:20:38 +00:00
|
|
|
/// <summary>Exception that has occured in the background process</summary>
|
|
|
|
private volatile Exception exception;
|
2007-07-05 20:02:02 +00:00
|
|
|
|
2007-04-16 17:18:16 +00:00
|
|
|
}
|
2007-07-04 19:19:48 +00:00
|
|
|
|
2007-07-01 19:27:40 +00:00
|
|
|
} // namespace Nuclex.Support.Scheduling
|