Found a probably good implementation for the Operation class

git-svn-id: file:///srv/devel/repo-conversion/nusu@33 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-07-04 19:19:48 +00:00
parent b73d7846d8
commit 46c0ac68af
3 changed files with 106 additions and 9 deletions

View File

@ -27,8 +27,57 @@ namespace Nuclex.Support.Scheduling {
/// <summary>Base class for observable operations running in the background</summary>
public abstract class Operation : Progression {
/// <summary>Executes the operation synchronously</summary>
public virtual void Execute() {
Begin();
End();
}
/// <summary>Launches the background operation</summary>
public abstract void Start();
public abstract void Begin();
/// <summary>Waits for the background operation to end</summary>
/// <remarks>
/// Any exceptions raised in the background operation will be thrown
/// in this method.
/// </remarks>
public virtual void End() {
// Use some ingenious double-checked locking to set the endCalled flag.
// Quite a lot of effort for a mere safety feature that prevents the programmer
// from calling End() twice.
bool error;
if(!this.endCalled) {
lock(this) {
if(!this.endCalled) {
this.endCalled = true;
error = false;
} else {
error = true;
}
}
} else {
error = true;
}
// If the progression itself hasn't ended yet, block the caller until it has.
if(!Ended)
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
/// in the background process. When it is set, the End() method will
/// </remarks>
protected Exception occuredException;
/// <summary>Whether the End() method has been called already</summary>
private volatile bool endCalled;
}

View File

@ -19,10 +19,62 @@ License along with this library
#endregion
using System;
using System.Collections.Generic;
using System.Threading;
namespace Nuclex.Support.Scheduling {
/*
/// <summary>Operation that executes a method in a background thread</summary>
public class ThreadedMethodOperation : Operation {
/// <summary>
/// Initializes a new threaded method operation for a parameterless method
/// </summary>
/// <param name="method">Method to be invoked in a background thread</param>
/// <remarks>
/// Uses a ThreadPool thread to execute the method in
/// </remarks>
public ThreadedMethodOperation(ThreadStart method)
: this(method, true) { }
/// <summary>
/// Initializes a new threaded method operation for a parameterless method
/// </summary>
/// <param name="method">Method to be invoked in a background thread</param>
/// <param name="useThreadPool">Whether to use a ThreadPool thread</param>
/// <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>
public ThreadedMethodOperation(ThreadStart method, bool useThreadPool) {
if(useThreadPool) {
ThreadPool.QueueUserWorkItem(callMethod, method);
} else {
Thread thread = new Thread(callMethod);
thread.Name = "Nuclex.Support.Scheduling.ThreadedMethodOperation thread";
thread.IsBackground = true;
thread.Start(method);
}
}
/// <summary>Invokes the delegate passed as an argument</summary>
/// <param name="method">ThreadStart-comaptible Delegate to invoke</param>
private void callMethod(object method) {
#if PROGRESSION_STARTABLE
AsyncStarted();
#endif
try {
((ThreadStart)method)();
}
catch(Exception exception) {
this.occuredException = exception;
}
finally {
AsyncEnded();
}
}
}
*/
} // namespace Nuclex.Support.Scheduling

View File

@ -69,7 +69,6 @@ namespace Nuclex.Support.Tracking {
: this() {
// Construct an ObservedProgression around each of the WeightedProgressions
float totalWeight = 0.0f;
foreach(WeightedProgression<ProgressionType> progression in childs) {
this.childs.Add(
new ObservedProgression<ProgressionType>(
@ -80,12 +79,9 @@ namespace Nuclex.Support.Tracking {
);
// Sum up the total weight
totalWeight += progression.Weight;
this.totalWeight += progression.Weight;
}
// Take over the summed weight of all progressions we were given
this.totalWeight = totalWeight;
}
/// <summary>Immediately releases all resources owned by the object</summary>
@ -150,7 +146,7 @@ namespace Nuclex.Support.Tracking {
}
/// <summary>
/// Called when an observed progressions ends
/// Called when an observed progression ends
/// </summary>
private void asyncEnded() {