Provided a common class for observable collections similar to what the .NET Framework 3.0 offers; ParentingCollection no longer requires the parent type to be a reference type; Laid the foundation for a new progress tracking framework that can be used to drive progress bars, loading screens and such

git-svn-id: file:///srv/devel/repo-conversion/nusu@9 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-04-16 17:18:16 +00:00
parent 355e5766d4
commit 2d145ca867
11 changed files with 379 additions and 6 deletions

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/// <summary>Interface for abortable processes</summary>
public interface IAbortable {
/// <summary>Aborts the running process. Can be called from any thread.</summary>
void AsyncAbort();
}
} // namespace Nuclex.Support.Tracking

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
public class MultiProgression<ProgressionType> : Progression
where ProgressionType : Progression {
}
} // namespace Nuclex.Support.Tracking

View file

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/// <summary>Base class for observable operations running in the background</summary>
public abstract class Operation : Progression {
/// <summary>Possible outcomes of an operation</summary>
public enum Outcomes {
/// <summary>The operation has not ended yet</summary>
Pending,
/// <summary>The operation has succeeded</summary>
Success,
/// <summary>The operation has failed</summary>
Failure,
}
/*
/// <summary>Begins executing the operation</summary>
public abstract void BeginExecute();
public virtual void Execute() {
try {
BeginExecute();
this.outcome = EndExecute();
}
catch(Exception exception) {
this.outcome = Outcomes.Failure;
}
}
/// <summary>Ends the </summary>
public virtual Outcomes EndExecute() {
WaitHandle.WaitOne();
return this.outcome;
}
/// <summary>The Outcome of the operation when it has finished</summary>
public Outcomes Outcome {
get { return this.outcome; }
}
/// <summary>Outcome of the operation</summary>
protected Outcomes outcome;
*/
}
} // namespace Nuclex.Support.Tracking

View file

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/// <summary>Event arguments for a progress update notification</summary>
public class ProgressUpdateEventArgs : EventArgs {
/// <summary>Initializes the progress update informations</summary>
/// <param name="progress">Achieved progress ranging from 0.0 to 1.0</param>
public ProgressUpdateEventArgs(float progress) {
this.progress = progress;
}
/// <summary>Currently achieved progress</summary>
public float Progress {
get { return this.progress; }
}
/// <summary>Achieved progress</summary>
protected float progress;
}
} // namespace Nuclex.Support.Tracking

View file

@ -0,0 +1,128 @@
using System;
using System.Threading;
namespace Nuclex.Support.Tracking {
/// <summary>Base class for actions that give an indication of their progress</summary>
/// <remarks>
/// <para>
/// By encapsulating long-running operations which will ideally be running in
/// a background thread in a class that's derived from Progression you can wait
/// for the completion of the operation and receive feedback on the achieved
/// progress. This is useful for displaying a progress bar, loading screen or
/// some means of entertaining the user while he waits for the operation to
/// complete. It is also possible to register callbacks which will be fired once
/// the progression has ended.
/// </para>
/// <para>
/// This class deliberately does not provide an Execute() or similar method to
/// clearly seperate the initiation of an operation from just monitoring it.
/// By omitting an Execute() method, it also becomes possible to construct a
/// progression just-in-time when it is explicitely asked for.
/// </para>
/// </remarks>
public abstract class Progression {
#region class EndedDummyProgression
/// <summary>Dummy progression which always is in the 'ended' state</summary>
internal class EndedDummyProgression : Progression {
/// <summary>Initializes a new ended dummy progression</summary>
public EndedDummyProgression() {
OnAsyncEnded();
}
}
#endregion // class EndedDummyProgression
/// <summary>A dummy progression that's always in the 'ended' state</summary>
/// <remarks>
/// Useful if an operation is already complete when it's being asked for or
/// when a progression that's lazily created is accessed after the original
/// operation has ended already.
/// </remarks>
public static readonly Progression EndedDummy = new EndedDummyProgression();
/// <summary>will be triggered to report when progress has been achieved</summary>
public event EventHandler<ProgressUpdateEventArgs> AsyncProgressUpdated;
/// <summary>Will be triggered when the progression has ended</summary>
public event EventHandler AsyncEnded;
/// <summary>Whether the progression has ended already</summary>
public virtual bool Ended {
get { return ended; }
}
/// <summary>WaitHandle that can be used to wait for the progression to end</summary>
public WaitHandle WaitHandle {
get {
lock(this) {
// The WaitHandle will only be created when someone asks for it!
if(this.doneEvent == null)
this.doneEvent = new ManualResetEvent(this.ended);
}
return this.doneEvent;
}
}
/// <summary>Fires the progress update event</summary>
/// <param name="progress">Progress to report (ranging from 0.0 to 1.0)</param>
/// <remarks>
/// Informs the observers of this progression about the achieved progress.
/// </remarks>
protected virtual void OnAsyncProgressUpdated(float progress) {
OnAsyncProgressUpdated(new ProgressUpdateEventArgs(progress));
}
/// <summary>Fires the progress update event</summary>
/// <param name="eventArguments">Progress to report (ranging from 0.0 to 1.0)</param>
/// <remarks>
/// Informs the observers of this progression about the achieved progress.
/// Allows for classes derived from the Progression class to easily provide
/// a custom event arguments class that has been derived from the
/// Progression's ProgressUpdateEventArgs class.
/// </remarks>
protected virtual void OnAsyncProgressUpdated(ProgressUpdateEventArgs eventArguments) {
EventHandler<ProgressUpdateEventArgs> copy = AsyncProgressUpdated;
if(copy != null)
copy(this, eventArguments);
}
/// <summary>Fires the AsyncEnded event</summary>
/// <remarks>
/// This event should be fired by the implementing class when its work is completed.
/// It's of no interest to this class whether the outcome of the process was successfull
/// or not, the outcome and results of the process taking place need to be communicated
/// seperately.
/// </remarks>
protected virtual void OnAsyncEnded() {
lock(this) {
ended = true;
if(this.doneEvent != null)
this.doneEvent.Set();
}
EventHandler copy = AsyncEnded;
if(copy != null)
copy(this, EventArgs.Empty);
}
/// <summary>Event that will be set when the progression is completed</summary>
/// <remarks>
/// This event is will only be created when it is specifically asked for using
/// the WaitHandle property.
/// </remarks>
private volatile ManualResetEvent doneEvent;
/// <summary>Whether the operation has completed yet</summary>
private volatile bool ended;
}
} // namespace Nuclex.Support.Tracking

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Nuclex.Support.Collections;
namespace Nuclex.Support.Tracking {
/// <summary>Non-generic version of the progression collection</summary>
public class ProgressionCollection : ProgressionCollection<Progression> { }
/// <summary>Generic collection of progressions</summary>
public class ProgressionCollection<ProgressionType>
: ObservableCollection<Progression> where ProgressionType : Progression { }
} // namespace Nuclex.Support.Tracking

View file

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
public class ThreadedMethodOperation : Operation {
}
} // namespace Nuclex.Support.Tracking