2008-03-26 19:52:28 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2009-01-07 19:05:29 +00:00
|
|
|
Copyright (C) 2002-2009 Nuclex Development Labs
|
2008-03-26 19:52:28 +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
|
|
|
|
|
2008-02-07 21:25:34 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Tracking {
|
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
/// <summary>Asynchronous request running in the background</summary>
|
2008-02-07 21:25:34 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// <para>
|
|
|
|
/// If the background process fails, the exception that caused it to fail is
|
2008-06-11 20:28:08 +00:00
|
|
|
/// communicated to all parties waiting on the Request through the Join()
|
|
|
|
/// method. Implementers should store any errors occuring in the asynchronous
|
|
|
|
/// parts of their code in a try..catch block (or avoid throwing and just
|
|
|
|
/// store a new exception) and re-throw them when in ReraiseExceptions()
|
2008-02-07 21:25:34 +00:00
|
|
|
/// </para>
|
|
|
|
/// <para>
|
2008-12-03 18:58:20 +00:00
|
|
|
/// Like in the transaction class, the contract requires you to always call
|
2008-06-11 20:28:08 +00:00
|
|
|
/// OnAsyncEnded(), no matter what the outcome of your operation is.
|
2008-02-07 21:25:34 +00:00
|
|
|
/// </para>
|
|
|
|
/// </remarks>
|
2008-12-03 18:58:20 +00:00
|
|
|
public abstract class Request : Transaction {
|
2008-02-07 21:25:34 +00:00
|
|
|
|
2008-03-26 19:52:28 +00:00
|
|
|
#region class EndedDummyRequest
|
2008-02-07 21:25:34 +00:00
|
|
|
|
2008-03-26 19:52:28 +00:00
|
|
|
/// <summary>Dummy request that is always in the ended state</summary>
|
|
|
|
private class EndedDummyRequest : Request {
|
|
|
|
/// <summary>Creates a new successfully completed dummy request</summary>
|
|
|
|
public EndedDummyRequest() : this(null) { }
|
|
|
|
/// <summary>Creates a new failed dummy request</summary>
|
2008-02-07 21:25:34 +00:00
|
|
|
/// <param name="exception">Exception that caused the dummy to fail</param>
|
2008-03-26 19:52:28 +00:00
|
|
|
public EndedDummyRequest(Exception exception) {
|
2008-03-26 20:20:38 +00:00
|
|
|
this.exception = exception;
|
2008-02-07 21:25:34 +00:00
|
|
|
OnAsyncEnded();
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
/// <summary>Exception that supposedly caused the request to fail</summary>
|
|
|
|
private Exception exception;
|
2008-02-07 21:25:34 +00:00
|
|
|
}
|
|
|
|
|
2008-03-26 19:52:28 +00:00
|
|
|
#endregion // EndedDummyRequest
|
2008-02-07 21:25:34 +00:00
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
/// <summary>Succeeded dummy request</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Use to indicate success if the request has already been completed at
|
|
|
|
/// the time you are asked to perform it.
|
|
|
|
/// </remarks>
|
|
|
|
public static readonly Request SucceededDummy = new EndedDummyRequest();
|
|
|
|
|
2008-03-26 19:52:28 +00:00
|
|
|
/// <summary>Creates a new failed dummy request</summary>
|
2008-03-26 20:20:38 +00:00
|
|
|
/// <param name="exception">Exception that supposedly caused the request to fail</param>
|
2008-02-07 21:25:34 +00:00
|
|
|
/// <returns>
|
2008-03-26 19:52:28 +00:00
|
|
|
/// A failed request that reports the provided exception as cause for its failure
|
2008-02-07 21:25:34 +00:00
|
|
|
/// </returns>
|
2008-06-05 19:19:00 +00:00
|
|
|
public static Request CreateFailedDummy(Exception exception) {
|
2008-03-26 19:52:28 +00:00
|
|
|
return new EndedDummyRequest(exception);
|
2008-02-07 21:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Waits for the background operation to end</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Any exceptions raised in the background operation will be thrown
|
|
|
|
/// in this method. If you decide to override this method, you should
|
2008-08-14 21:14:40 +00:00
|
|
|
/// call Wait() first (and let any possible exception through to your
|
2008-02-07 21:25:34 +00:00
|
|
|
/// caller).
|
|
|
|
/// </remarks>
|
|
|
|
public virtual void Join() {
|
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
// If the request itself hasn't ended yet, block the caller until it has.
|
2008-06-11 20:06:23 +00:00
|
|
|
// We could just use WaitHandle.WaitOne() here, but since the WaitHandle is created
|
|
|
|
// on-the-fly only when it is requested, we can avoid the WaitHandle creation in
|
|
|
|
// case the request is already finished!
|
2008-02-07 21:25:34 +00:00
|
|
|
if(!Ended)
|
2008-06-11 20:06:23 +00:00
|
|
|
Wait();
|
2008-02-07 21:25:34 +00:00
|
|
|
|
2008-08-14 21:14:40 +00:00
|
|
|
// Allow the implementer to throw an exception in case an error has occured
|
2008-03-26 20:20:38 +00:00
|
|
|
ReraiseExceptions();
|
2008-02-07 21:25:34 +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>
|
2008-05-14 19:06:06 +00:00
|
|
|
protected virtual void ReraiseExceptions() { }
|
|
|
|
|
2008-03-26 20:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Request providing a result that can be passed to the caller</summary>
|
|
|
|
/// <typeparam name="ResultType">
|
|
|
|
/// Type of the result being provided by the request
|
|
|
|
/// </typeparam>
|
|
|
|
public abstract class Request<ResultType> : Request {
|
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
#region class SucceededDummyRequest
|
2008-03-26 20:20:38 +00:00
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
/// <summary>Succeeded dummy request that is always in the ended state</summary>
|
|
|
|
private class SucceededDummyRequest : Request<ResultType> {
|
|
|
|
/// <summary>Creates a new failed dummy request</summary>
|
|
|
|
/// <param name="result">Result to return to the request's caller</param>
|
|
|
|
public SucceededDummyRequest(ResultType result) {
|
|
|
|
this.result = result;
|
|
|
|
OnAsyncEnded();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Allows the specific request implementation to re-throw an exception if
|
|
|
|
/// the background process finished unsuccessfully
|
|
|
|
/// </summary>
|
|
|
|
protected override ResultType GatherResults() {
|
|
|
|
return this.result;
|
|
|
|
}
|
|
|
|
/// <summary>Results the succeede dummy request will provide to the caller</summary>
|
|
|
|
private ResultType result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion // SucceededDummyRequest
|
|
|
|
|
|
|
|
#region class FailedDummyRequest
|
|
|
|
|
|
|
|
/// <summary>Failed dummy request that is always in the ended state</summary>
|
|
|
|
private class FailedDummyRequest : Request<ResultType> {
|
2008-03-26 20:20:38 +00:00
|
|
|
/// <summary>Creates a new failed dummy request</summary>
|
|
|
|
/// <param name="exception">Exception that caused the dummy to fail</param>
|
2008-06-11 20:28:08 +00:00
|
|
|
public FailedDummyRequest(Exception exception) {
|
2008-03-26 20:20:38 +00:00
|
|
|
this.exception = exception;
|
|
|
|
OnAsyncEnded();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Allows the specific request implementation to re-throw an exception if
|
|
|
|
/// the background process finished unsuccessfully
|
|
|
|
/// </summary>
|
|
|
|
protected override ResultType GatherResults() {
|
|
|
|
throw this.exception;
|
|
|
|
}
|
|
|
|
/// <summary>Exception that supposedly caused the request to fail</summary>
|
|
|
|
private Exception exception;
|
2008-02-07 21:25:34 +00:00
|
|
|
}
|
|
|
|
|
2008-06-11 20:28:08 +00:00
|
|
|
#endregion // FailedDummyRequest
|
|
|
|
|
|
|
|
/// <summary>Creates a new failed dummy request</summary>
|
|
|
|
/// <param name="result">Result to provide to the caller</param>
|
|
|
|
/// <returns>
|
|
|
|
/// A succeeded request that returns the provided result to the caller
|
|
|
|
/// </returns>
|
|
|
|
public static Request<ResultType> CreateSucceededDummy(ResultType result) {
|
|
|
|
return new SucceededDummyRequest(result);
|
|
|
|
}
|
2008-03-26 20:20:38 +00:00
|
|
|
|
|
|
|
/// <summary>Creates a new failed dummy request</summary>
|
|
|
|
/// <param name="exception">Exception that supposedly caused the request to fail</param>
|
|
|
|
/// <returns>
|
|
|
|
/// A failed request that reports the provided exception as cause for its failure
|
|
|
|
/// </returns>
|
2008-06-11 20:28:08 +00:00
|
|
|
public static new Request<ResultType> CreateFailedDummy(Exception exception) {
|
|
|
|
return new FailedDummyRequest(exception);
|
2008-03-26 20:20:38 +00:00
|
|
|
}
|
2008-02-07 21:25:34 +00:00
|
|
|
|
2008-03-26 20:20:38 +00:00
|
|
|
/// <summary>Waits for the background operation to end</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Any exceptions raised in the background operation will be thrown
|
|
|
|
/// in this method. If you decide to override this method, you should
|
|
|
|
/// call End() first (and let any possible exception through to your
|
|
|
|
/// caller).
|
|
|
|
/// </remarks>
|
|
|
|
public new ResultType Join() {
|
|
|
|
base.Join();
|
2008-02-07 21:25:34 +00:00
|
|
|
|
2008-03-26 20:20:38 +00:00
|
|
|
// Return the results of the request
|
|
|
|
return GatherResults();
|
2008-02-07 21:25:34 +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>
|
2009-05-20 20:14:21 +00:00
|
|
|
protected override void ReraiseExceptions() {
|
|
|
|
// Request and discard the result, so the implementor can do all error handling
|
|
|
|
// in the GatherResults() method. This is a good default implementation as long
|
|
|
|
// as the returned object does not require IDispose. It if does, this method
|
|
|
|
// needs to be overridden.
|
|
|
|
GatherResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Allows the specific request to return the results of the Request to the
|
|
|
|
/// caller of the Join() method
|
|
|
|
/// </summary>
|
2008-03-26 20:20:38 +00:00
|
|
|
protected abstract ResultType GatherResults();
|
2008-02-07 21:25:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Tracking
|