Began implementing the ProgressionTracker

git-svn-id: file:///srv/devel/repo-conversion/nusu@34 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-07-05 20:02:02 +00:00
parent 46c0ac68af
commit 344e5fac53
11 changed files with 363 additions and 66 deletions

View file

@ -27,12 +27,6 @@ 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 Begin();
@ -55,13 +49,15 @@ namespace Nuclex.Support.Scheduling {
} else {
error = true;
}
}
} // lock
} else {
error = true;
}
if(error)
throw new InvalidOperationException("End() has already been called");
// If the progression itself hasn't ended yet, block the caller until it has.
if(!Ended)
if(!Ended)
WaitHandle.WaitOne();
// If an exception occured during the background execution
@ -75,7 +71,25 @@ namespace Nuclex.Support.Scheduling {
/// 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;
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
// handling block and locking the operation instance could cause all even
// more problems.
this.occuredException = exception;
}
/// <summary>Exception that occured while the operation was executing</summary>
private volatile Exception occuredException;
/// <summary>Whether the End() method has been called already</summary>
private volatile bool endCalled;

View file

@ -0,0 +1,68 @@
#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
using System;
using System.Collections.Generic;
using Nuclex.Support.Tracking;
namespace Nuclex.Support.Scheduling {
/// <summary>Operation that sequentially executes a series of operations</summary>
/// <typeparam name="OperationType">
/// Type of the child operations the QueueOperation will contain
/// </typeparam>
public class QueueOperation<OperationType> : Operation
where OperationType : Operation {
/// <summary>Initializes a new queue operation</summary>
/// <param name="childs">Child operations to execute in this operation</param>
/// <remarks>
/// All child operations will have a default weight of 1.0
/// </remarks>
public QueueOperation(IEnumerable<OperationType> childs) {
this.setProgression = new SetProgression<OperationType>(childs);
}
/// <summary>Initializes a new queue operation with custom weights</summary>
/// <param name="childs">Child operations to execute in this operation</param>
public QueueOperation(IEnumerable<WeightedProgression<OperationType>> childs) {
this.setProgression = new SetProgression<OperationType>(childs);
}
/// <summary>Launches the background operation</summary>
public override void Begin() {
this.setProgression.AsyncProgressUpdated +=
new EventHandler<ProgressUpdateEventArgs>(setProgressionProgressUpdated);
//this.setProgression.Childs[0].Progression.Begin();
}
private void setProgressionProgressUpdated(object sender, ProgressUpdateEventArgs e) {
throw new Exception("The method or operation is not implemented.");
}
/// <summary>SetProgression used internally to handle progress reports</summary>
private volatile SetProgression<OperationType> setProgression;
}
} // namespace Nuclex.Support.Scheduling

View file

@ -24,7 +24,7 @@ using System.Threading;
namespace Nuclex.Support.Scheduling {
/// <summary>Operation that executes a method in a background thread</summary>
public class ThreadedMethodOperation : Operation {
public class ThreadCallbackOperation : ThreadOperation {
/// <summary>
/// Initializes a new threaded method operation for a parameterless method
@ -33,7 +33,7 @@ namespace Nuclex.Support.Scheduling {
/// <remarks>
/// Uses a ThreadPool thread to execute the method in
/// </remarks>
public ThreadedMethodOperation(ThreadStart method)
public ThreadCallbackOperation(ThreadStart method)
: this(method, true) { }
/// <summary>
@ -46,35 +46,20 @@ namespace Nuclex.Support.Scheduling {
/// 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);
}
public ThreadCallbackOperation(ThreadStart method, bool useThreadPool)
: base(useThreadPool) {
this.method = 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();
}
/// <summary>Executes the thread callback in the background thread</summary>
protected override void Execute() {
this.method();
}
/// <summary>Method to be invoked in a background thread</summary>
private ThreadStart method;
}
} // namespace Nuclex.Support.Scheduling

View file

@ -0,0 +1,88 @@
#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
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 abstract class ThreadOperation : Operation {
/// <summary>
/// Initializes a new threaded operation
/// </summary>
/// <remarks>
/// Uses a ThreadPool thread to execute the method in
/// </remarks>
public ThreadOperation() : this(true) { }
/// <summary>
/// Initializes a new threaded operation
/// </summary>
/// <param name="useThreadPool">Whether tos 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 ThreadOperation(bool useThreadPool) {
this.useThreadPool = useThreadPool;
}
/// <summary>Launches the background operation</summary>
public override void Begin() {
if(useThreadPool) {
ThreadPool.QueueUserWorkItem(callMethod);
} else {
Thread thread = new Thread(callMethod);
thread.Name = "Nuclex.Support.Scheduling.ThreadOperation";
thread.IsBackground = true;
thread.Start();
}
}
/// <summary>Contains the payload to be executed in the background thread</summary>
protected abstract void Execute();
/// <summary>Invokes the delegate passed as an argument</summary>
/// <param name="state">Not used</param>
private void callMethod(object state) {
#if PROGRESSION_STARTABLE
OnAsyncStarted();
#endif
try {
Execute();
}
catch(Exception exception) {
SetException(exception);
}
finally {
OnAsyncEnded();
}
}
/// <summary>Whether to use the ThreadPool for obtaining a background thread</summary>
private bool useThreadPool;
}
} // namespace Nuclex.Support.Scheduling