|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Collections.ObjectModel;
using HiveStudios.EventBroker;
using Interfaces;
using CommonData;
namespace CommonUtilities
{
public class CommonThreadsManager : IThreadsManager
{
/// <summary>
/// The threads dictionary
/// </summary>
private readonly Dictionary<IThreadsCreateData, System.Threading.Thread> _theThreads;
/// <summary>
/// The _threads countdown event to keep track of thread termination
/// </summary>
private CountdownEvent _threadsCountdown;
/// <summary>
/// Constructor
/// </summary>
public CommonThreadsManager()
{
_theThreads = new Dictionary<IThreadsCreateData, System.Threading.Thread>();
}
#region IThreadsManager Members
/// <summary>
/// Gets the threads count down object.
/// </summary>
/// <value>
/// The threads count down.
/// </value>
public CountdownEvent ThreadsCountDown
{
get
{
return _threadsCountdown;
}
}
/// <summary>
/// Return the current Thread Count
/// </summary>
public int ActiveThreadsCount
{
get
{
if (_theThreads == null) return 0;
return _theThreads.Count;
}
}
/// <summary>
/// Return the currently active threads IThreadCreateData objects
/// </summary>
public ReadOnlyCollection<IThreadsCreateData> ActiveThreadsData
{
get { return _theThreads.Keys.ToList().AsReadOnly(); }
}
/// <summary>
/// Return the currently active threads names
/// </summary>
public ReadOnlyCollection<string> ActiveThreadsNames
{
get { return _theThreads.Keys.Select(activeThreads => activeThreads.Name).ToList().AsReadOnly(); }
}
/// <summary>
/// Adds a thread to the thread collection by creating it and starting it.
/// </summary>
/// <param name="data"></param>
/// <param name="invokedMethod"></param>
/// <returns></returns>
public bool AddThread(IThreadsCreateData data, ParameterizedThreadStart invokedMethod)
{
bool status = true;
try
{
Thread tmpThread = new Thread(invokedMethod);
tmpThread.IsBackground = true;
_theThreads.Add(data, tmpThread);
tmpThread.Start(data);
if (tmpThread.IsAlive)
{
EventBroker.Execute(CommonEventsMessages.MapThreadStarted, this, new GlobalEventEventArgs(data));
if (_threadsCountdown == null)
{
_threadsCountdown = new CountdownEvent(1);
}
else
{
_threadsCountdown.AddCount();
}
}
}
catch (Exception ex)
{
//Write to log
Log.ErrorMessage(string.Format("Failed To create Thread {0}", data.Name), ex);
status = false;
}
return status;
}
/// <summary>
/// Removes an active thread if exists
/// </summary>
/// <param name="name"></param>
/// <param name="message"></param>
/// <returns></returns>
public bool RemoveThread(IThreadsCreateData data, string message)
{
bool status = true;
try
{
if (_theThreads.Keys.Contains(data))
{
_theThreads.Remove(data);
_threadsCountdown.Signal();
}
}
catch (Exception ex)
{
Log.ErrorMessage(string.Format("Failed To remove Thread {0}", data.Name), ex);
status = false;
}
return status;
}
/// <summary>
/// Stops all active threads
/// </summary>
/// <returns></returns>
public bool StopAllThreads()
{
bool status = true;
//foreach (string name in _theThreads.Keys)
//{
// RemoveThread(name, string.Format("Thread {0} was stopped as part of Stop All Threads request.", name));
//}
return status;
}
/// <summary>
/// Determines whether the specified thread name is alive.
/// </summary>
/// <param name="threadName">Name of the thread.</param>
/// <returns></returns>
public bool IsAlive(IThreadsCreateData data)
{
try
{
if (_theThreads.Keys.Contains(data))
{
return _theThreads[data].IsAlive;
}
}
catch (Exception ex)
{
Log.ErrorMessage(string.Format("Failed retreieve status Thread {0}", data.Name), ex);
}
return false;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Project Manager and Application Developer, in a wide variety of business applications. Particularly interested in client/server and Graphical User Interface design using Visual C#.
Specialties: 13 Y'rs in C#, 10 Y'rs experience in C++ Highly experienced in wide technologies, IT projects, military projects etc'.