2007-05-16 20:28:23 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
|
|
|
Copyright (C) 2002-2007 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
|
2007-07-24 20:15:19 +00:00
|
|
|
|
2007-04-16 17:18:16 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2007-07-01 19:27:40 +00:00
|
|
|
using Nuclex.Support.Tracking;
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Scheduling {
|
2007-04-16 17:18:16 +00:00
|
|
|
|
|
|
|
/// <summary>Base class for observable operations running in the background</summary>
|
|
|
|
public abstract class Operation : Progression {
|
|
|
|
|
2007-07-01 19:27:40 +00:00
|
|
|
/// <summary>Launches the background operation</summary>
|
2007-07-04 19:19:48 +00:00
|
|
|
public abstract void Begin();
|
|
|
|
|
|
|
|
/// <summary>Waits for the background operation to end</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Any exceptions raised in the background operation will be thrown
|
2007-07-10 18:15:34 +00:00
|
|
|
/// in this method. If you decide to override this method, you should
|
|
|
|
/// call End() first (and let any possible exception through to your
|
|
|
|
/// caller).
|
2007-07-04 19:19:48 +00:00
|
|
|
/// </remarks>
|
|
|
|
public virtual void End() {
|
|
|
|
|
2007-07-12 22:16:11 +00:00
|
|
|
// By design, end can only be called once!
|
|
|
|
lock(this) {
|
|
|
|
if(this.endCalled)
|
|
|
|
throw new InvalidOperationException("End() has already been called");
|
|
|
|
|
|
|
|
this.endCalled = true;
|
2007-07-04 19:19:48 +00:00
|
|
|
}
|
2007-07-05 20:02:02 +00:00
|
|
|
|
2007-07-04 19:19:48 +00:00
|
|
|
// If the progression itself hasn't ended yet, block the caller until it has.
|
2007-07-05 20:02:02 +00:00
|
|
|
if(!Ended)
|
2007-07-04 19:19:48 +00:00
|
|
|
WaitHandle.WaitOne();
|
|
|
|
|
|
|
|
// If an exception occured during the background execution
|
|
|
|
if(this.occuredException != null)
|
|
|
|
throw this.occuredException;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Exception that occured while the operation was executing</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// If this field is null, it is assumed that no exception has occured
|
2007-07-09 21:41:21 +00:00
|
|
|
/// in the background process. If it is set, however, the End() method will
|
|
|
|
/// re-raise the exception to the calling thread when it is called.
|
2007-07-04 19:19:48 +00:00
|
|
|
/// </remarks>
|
2007-07-05 20:02:02 +00:00
|
|
|
public Exception OccuredException {
|
|
|
|
get { return this.occuredException; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Sets the exception to raise to the caller of the End() method</summary>
|
|
|
|
/// <param name="exception">Exception to raise to the caller of the End() method</param>
|
|
|
|
protected void SetException(Exception exception) {
|
|
|
|
|
|
|
|
// We allow the caller to set the exception multiple times. While I certainly
|
|
|
|
// 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
|
2007-07-10 18:15:34 +00:00
|
|
|
// handling block and locking + throwing here could cause all kinds of problems.
|
2007-07-05 20:02:02 +00:00
|
|
|
this.occuredException = exception;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Exception that occured while the operation was executing</summary>
|
|
|
|
private volatile Exception occuredException;
|
2007-07-04 19:19:48 +00:00
|
|
|
/// <summary>Whether the End() method has been called already</summary>
|
|
|
|
private volatile bool endCalled;
|
2007-05-02 19:25:21 +00:00
|
|
|
|
2007-04-16 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 19:27:40 +00:00
|
|
|
} // namespace Nuclex.Support.Scheduling
|