Trimmed down the Request class and provided it with a clear way to pass results to the user of the request

git-svn-id: file:///srv/devel/repo-conversion/nusu@63 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2008-03-26 20:20:38 +00:00
parent c21ba759cc
commit 1161ef8f25
4 changed files with 114 additions and 36 deletions

View File

@ -109,7 +109,7 @@ namespace Nuclex.Support.Scheduling {
/// <summary>Moves the operation into the ended state with an exception</summary> /// <summary>Moves the operation into the ended state with an exception</summary>
/// <param name="exception">Exception</param> /// <param name="exception">Exception</param>
public void SetEnded(Exception exception) { public void SetEnded(Exception exception) {
SetException(exception); this.exception = exception;
OnAsyncEnded(); OnAsyncEnded();
} }
@ -121,6 +121,18 @@ namespace Nuclex.Support.Scheduling {
OnAsyncProgressUpdated(progress); OnAsyncProgressUpdated(progress);
} }
/// <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 has occured in the background process</summary>
private volatile Exception exception;
} }
#endregion // class TestOperation #endregion // class TestOperation

View File

@ -84,6 +84,15 @@ namespace Nuclex.Support.Scheduling {
startCurrentOperation(); startCurrentOperation();
} }
/// <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>Prepares the current operation and calls its Begin() method</summary> /// <summary>Prepares the current operation and calls its Begin() method</summary>
/// <remarks> /// <remarks>
/// This subscribes the queue to the events of to the current operation /// This subscribes the queue to the events of to the current operation
@ -121,7 +130,7 @@ namespace Nuclex.Support.Scheduling {
OnAsyncProgressUpdated(this.completedWeight / this.totalWeight); OnAsyncProgressUpdated(this.completedWeight / this.totalWeight);
} }
catch(Exception exception) { catch(Exception exception) {
SetException(exception); this.exception = exception;
} }
} }
@ -135,7 +144,7 @@ namespace Nuclex.Support.Scheduling {
endCurrentOperation(); endCurrentOperation();
// Only jump to the next operation if no exception occured // Only jump to the next operation if no exception occured
if(OccuredException == null) { if(this.exception == null) {
++this.currentOperationIndex; ++this.currentOperationIndex;
@ -183,6 +192,8 @@ namespace Nuclex.Support.Scheduling {
private float completedWeight; private float completedWeight;
/// <summary>Index of the operation currently executing</summary> /// <summary>Index of the operation currently executing</summary>
private int currentOperationIndex; private int currentOperationIndex;
/// <summary>Exception that has occured in the background process</summary>
private volatile Exception exception;
} }

View File

@ -70,15 +70,26 @@ namespace Nuclex.Support.Scheduling {
Execute(); Execute();
} }
catch(Exception exception) { catch(Exception exception) {
SetException(exception); this.exception = exception;
} }
finally { finally {
OnAsyncEnded(); OnAsyncEnded();
} }
} }
/// <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>Whether to use the ThreadPool for obtaining a background thread</summary> /// <summary>Whether to use the ThreadPool for obtaining a background thread</summary>
private bool useThreadPool; private bool useThreadPool;
/// <summary>Exception that has occured in the background process</summary>
private volatile Exception exception;
} }

View File

@ -37,7 +37,7 @@ namespace Nuclex.Support.Tracking {
/// OnAsyncEnded(), no matter what the outcome of your background operation is. /// OnAsyncEnded(), no matter what the outcome of your background operation is.
/// </para> /// </para>
/// </remarks> /// </remarks>
public class Request : Progression { public abstract class Request : Progression {
#region class EndedDummyRequest #region class EndedDummyRequest
@ -48,21 +48,25 @@ namespace Nuclex.Support.Tracking {
/// <summary>Creates a new failed dummy request</summary> /// <summary>Creates a new failed dummy request</summary>
/// <param name="exception">Exception that caused the dummy to fail</param> /// <param name="exception">Exception that caused the dummy to fail</param>
public EndedDummyRequest(Exception exception) { public EndedDummyRequest(Exception exception) {
this.exception = exception;
OnAsyncEnded(); OnAsyncEnded();
// Only call SetException() if an actual exception was provided. Who knows what
// evil code might be inside SetException() after all ;)
if(exception != null)
SetException(exception);
} }
/// <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;
} }
#endregion // EndedDummyRequest #endregion // EndedDummyRequest
/// <summary>Creates a new failed dummy request</summary> /// <summary>Creates a new failed dummy request</summary>
/// <param name="error"> /// <param name="exception">Exception that supposedly caused the request to fail</param>
/// Exception that supposedly caused the progression to fail
/// </param>
/// <returns> /// <returns>
/// A failed request that reports the provided exception as cause for its failure /// A failed request that reports the provided exception as cause for its failure
/// </returns> /// </returns>
@ -83,36 +87,76 @@ namespace Nuclex.Support.Tracking {
if(!Ended) if(!Ended)
WaitHandle.WaitOne(); WaitHandle.WaitOne();
// If an exception occured during the background execution // Allow the implementor to throw an exception in case an error has occured
if(this.occuredException != null) ReraiseExceptions();
throw this.occuredException;
} }
/// <summary>Exception that occured while the operation was executing</summary> /// <summary>
/// Allows the specific request implementation to re-throw an exception if
/// the background process finished unsuccessfully
/// </summary>
protected virtual void ReraiseExceptions() {}
}
/// <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 {
#region class EndedDummyRequest
/// <summary>Dummy request that is always in the ended state</summary>
private class EndedDummyRequest : Request<ResultType> {
/// <summary>Creates a new failed dummy request</summary>
/// <param name="exception">Exception that caused the dummy to fail</param>
public EndedDummyRequest(Exception exception) {
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;
}
#endregion // EndedDummyRequest
/// <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>
public static new Request CreateFailedDummyRequest(Exception exception) {
return new EndedDummyRequest(exception);
}
/// <summary>Waits for the background operation to end</summary>
/// <remarks> /// <remarks>
/// If this field is null, it is assumed that no exception has occured /// Any exceptions raised in the background operation will be thrown
/// in the background process. If it is set, however, the End() method will /// in this method. If you decide to override this method, you should
/// re-raise the exception to the calling thread when it is called. /// call End() first (and let any possible exception through to your
/// caller).
/// </remarks> /// </remarks>
public Exception OccuredException { public new ResultType Join() {
get { return this.occuredException; } base.Join();
// Return the results of the request
return GatherResults();
} }
/// <summary>Sets the exception to raise to the caller of the End() method</summary> /// <summary>
/// <param name="exception">Exception to raise to the caller of the End() method</param> /// Allows the specific request implementation to re-throw an exception if
protected void SetException(Exception exception) { /// the background process finished unsuccessfully
/// </summary>
// We allow the caller to set the exception multiple times. While I certainly protected abstract ResultType GatherResults();
// can't think of a scenario where this would happen, throwing an exception
// in that case seems worse. The caller might just be executing an exception
// handling block and locking + throwing here could cause all kinds of problems.
this.occuredException = exception;
}
/// <summary>Exception that occured while the operation was executing</summary>
private volatile Exception occuredException;
} }