The Shared helper is now obsolete (singletons are just too bad, not even suitable as workaround for static methods in interfaces for generic types; some cosmetic changes; made the ThreadRunner public
git-svn-id: file:///srv/devel/repo-conversion/nusu@333 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
2f82a2fdf9
commit
97183d2335
|
@ -47,8 +47,10 @@ namespace Nuclex.Support {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void TestSameInstance() {
|
public void TestSameInstance() {
|
||||||
|
#pragma warning disable 0618
|
||||||
Dummy dummyInstance = Shared<Dummy>.Instance;
|
Dummy dummyInstance = Shared<Dummy>.Instance;
|
||||||
Dummy otherDummyInstance = Shared<Dummy>.Instance;
|
Dummy otherDummyInstance = Shared<Dummy>.Instance;
|
||||||
|
#pragma warning restore 0618
|
||||||
|
|
||||||
// Make sure they're the same instance. We could have put an instance counter in
|
// Make sure they're the same instance. We could have put an instance counter in
|
||||||
// the dummy class, but this might or might not work well across multiple tests
|
// the dummy class, but this might or might not work well across multiple tests
|
||||||
|
|
|
@ -30,6 +30,7 @@ namespace Nuclex.Support {
|
||||||
public static class Shared<TShared> where TShared : new() {
|
public static class Shared<TShared> where TShared : new() {
|
||||||
|
|
||||||
/// <summary>Returns the global instance of the class</summary>
|
/// <summary>Returns the global instance of the class</summary>
|
||||||
|
[Obsolete("Avoid singletons at all costs. Consider a dependency injector instead.")]
|
||||||
public static TShared Instance {
|
public static TShared Instance {
|
||||||
[DebuggerStepThrough]
|
[DebuggerStepThrough]
|
||||||
get { return instance; }
|
get { return instance; }
|
||||||
|
|
|
@ -37,7 +37,9 @@ namespace Nuclex.Support.Threading {
|
||||||
#region class TestWorker
|
#region class TestWorker
|
||||||
|
|
||||||
/// <summary>Implementation of a background worker used for unit testing</summary>
|
/// <summary>Implementation of a background worker used for unit testing</summary>
|
||||||
|
#pragma warning disable 0618
|
||||||
private class TestWorker : ParallelBackgroundWorker<object> {
|
private class TestWorker : ParallelBackgroundWorker<object> {
|
||||||
|
#pragma warning restore 0618
|
||||||
|
|
||||||
/// <summary>Initializes a new parallel background worker with unlimited threads</summary>
|
/// <summary>Initializes a new parallel background worker with unlimited threads</summary>
|
||||||
public TestWorker() : base() { }
|
public TestWorker() : base() { }
|
||||||
|
|
|
@ -30,6 +30,7 @@ namespace Nuclex.Support.Threading {
|
||||||
|
|
||||||
/// <summary>Processes tasks in parallel using many threads</summary>
|
/// <summary>Processes tasks in parallel using many threads</summary>
|
||||||
/// <typeparam name="TTask">Type of tasks the class will process</typeparam>
|
/// <typeparam name="TTask">Type of tasks the class will process</typeparam>
|
||||||
|
[Obsolete("Use ThreadRunner instead; UI view models should inherit ThreadedViewModel")]
|
||||||
public abstract class ParallelBackgroundWorker<TTask> : IDisposable {
|
public abstract class ParallelBackgroundWorker<TTask> : IDisposable {
|
||||||
|
|
||||||
/// <summary>Number of CPU cores available on the system</summary>
|
/// <summary>Number of CPU cores available on the system</summary>
|
||||||
|
@ -61,7 +62,7 @@ namespace Nuclex.Support.Threading {
|
||||||
if(threadCount > 0) {
|
if(threadCount > 0) {
|
||||||
this.threadCount = threadCount;
|
this.threadCount = threadCount;
|
||||||
} else {
|
} else {
|
||||||
threadCount = Math.Max(1, ProcessorCount + threadCount);
|
this.threadCount = Math.Max(1, ProcessorCount + threadCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.queueSynchronizationRoot = new object();
|
this.queueSynchronizationRoot = new object();
|
||||||
|
@ -79,7 +80,7 @@ namespace Nuclex.Support.Threading {
|
||||||
/// <param name="name">Name that will be assigned to the worker threads</param>
|
/// <param name="name">Name that will be assigned to the worker threads</param>
|
||||||
public ParallelBackgroundWorker(string name) :
|
public ParallelBackgroundWorker(string name) :
|
||||||
this(int.MaxValue) {
|
this(int.MaxValue) {
|
||||||
threadName = name;
|
this.threadName = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -98,7 +99,7 @@ namespace Nuclex.Support.Threading {
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public ParallelBackgroundWorker(string name, int threadCount) :
|
public ParallelBackgroundWorker(string name, int threadCount) :
|
||||||
this(threadCount) {
|
this(threadCount) {
|
||||||
threadName = name;
|
this.threadName = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Immediately releases all resources owned by the instance</summary>
|
/// <summary>Immediately releases all resources owned by the instance</summary>
|
||||||
|
@ -317,7 +318,6 @@ namespace Nuclex.Support.Threading {
|
||||||
/// use this method of synchronization!
|
/// use this method of synchronization!
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
///
|
|
||||||
private object queueSynchronizationRoot;
|
private object queueSynchronizationRoot;
|
||||||
|
|
||||||
/// <summary>Delegate for the runQueuedTasksInThread() method</summary>
|
/// <summary>Delegate for the runQueuedTasksInThread() method</summary>
|
||||||
|
|
|
@ -29,7 +29,7 @@ using System.Threading.Tasks;
|
||||||
namespace Nuclex.Support.Threading {
|
namespace Nuclex.Support.Threading {
|
||||||
|
|
||||||
/// <summary>Executes actions in a threads</summary>
|
/// <summary>Executes actions in a threads</summary>
|
||||||
internal abstract class ThreadRunner : IDisposable {
|
public abstract class ThreadRunner : IDisposable {
|
||||||
|
|
||||||
#region interface IRunner
|
#region interface IRunner
|
||||||
|
|
||||||
|
|
|
@ -92,19 +92,6 @@ namespace Nuclex.Support {
|
||||||
return fieldInfos;
|
return fieldInfos;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds field informations to a list if they're not already contained in it
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="fieldInfos">List the field informations will be added to</param>
|
|
||||||
/// <param name="fieldInfo">Field informations that will be added to the list</param>
|
|
||||||
private static void addIfNotExists(
|
|
||||||
IDictionary<FieldInfo, object> fieldInfos, FieldInfo fieldInfo
|
|
||||||
) {
|
|
||||||
if(!fieldInfos.ContainsKey(fieldInfo)) {
|
|
||||||
fieldInfos.Add(fieldInfo, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Determines whether the given type has a default constructor</summary>
|
/// <summary>Determines whether the given type has a default constructor</summary>
|
||||||
/// <param name="type">Type which is to be checked</param>
|
/// <param name="type">Type which is to be checked</param>
|
||||||
/// <returns>True if the type has a default constructor</returns>
|
/// <returns>True if the type has a default constructor</returns>
|
||||||
|
@ -142,6 +129,19 @@ namespace Nuclex.Support {
|
||||||
return (attributes != null) && (attributes.Length > 0);
|
return (attributes != null) && (attributes.Length > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds field informations to a list if they're not already contained in it
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fieldInfos">List the field informations will be added to</param>
|
||||||
|
/// <param name="fieldInfo">Field informations that will be added to the list</param>
|
||||||
|
private static void addIfNotExists(
|
||||||
|
IDictionary<FieldInfo, object> fieldInfos, FieldInfo fieldInfo
|
||||||
|
) {
|
||||||
|
if(!fieldInfos.ContainsKey(fieldInfo)) {
|
||||||
|
fieldInfos.Add(fieldInfo, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Nuclex.Support
|
} // namespace Nuclex.Support
|
||||||
|
|
Loading…
Reference in New Issue
Block a user