• ThreadManager

Contents

ThreadManager

Namespace: Shared.Classes

Assembly: SharedLib

Thread Management class Manages threads, includes thread queing, basic statistics for thread on Process and System % CPU usage

Constructors

Properties

Cancelled Indicates wether the thread has been cancelled or not
TimeStart Date/time thread started
TimeFinish Date/time thread finished running
ContinueIfGlobalException if true and an unhandled error occurs then the thread will still continue to run
RunAtStartup If true then the thread will run straight away, if false then thread will wait for RunInterval
RunInterval How often the thread should run
SleepInterval Number of milliseconds the thread should sleep after running
Name Name of the thread
MarkedForRemoval Indicates the thread is finished and should be marked for removal from the collection
ThreadID Retrieves the thread ID
ID Actual ID for thread being watched
UnResponsive Indicates wether the thread was unresponsive or not
HangTimeout Hang Timeout for running thread A value of 0 (zero) indicates the thread will not be checked for hanging
ChildThreads Collectin of child threads
Parent Parent thread object
LastRun Date/Time the thread Run method was executed
ProcessCpuUsage Threads usage within the process
SystemCpuUsage Threads usage within the system
PreviousSystemCpuUsage Previous System CPU Usage
PreviousProcessCpuUsage Previous CPU Usage
CPUUsageChanged CPU Usage has changed
ThreadCount Returns the number of active threads
ThreadPoolCount Indicates the size of the thread pool
CancelRequested Indicates that a request to cancel all threads has been made
MaximumRunningThreads Sets the maximum number of threads that can be run
CheckForHangingThreads Determines wether checks are made for hanging threads, those that do not play nicely with others or appear incommunacative
AllowThreadPool Determines wether a thread pool is in operation
MaximumPoolSize Maximum size of the thread pool
ThreadHangTimeout Number of minutes a thread will timeout if it does not communicate and is deemed to have hanged
CpuUsage Retrieves the CPU Usage for the process
ProcessCpuOther Process usage for other threads, including Main Process thread and unmanaged threads
ThreadCpuChangeNotification Raises an event if Thread CPU usage changes by given percentage 0 = no notification 50 is maximum value

Methods

Get(Int32) Retrieves the Nth thread in the list
Exists(String) Checks wether a thread with a specific name already exists within the managed threads
Find(String) Checks wether a thread with a specific name exists and if so returns it
ThreadStart(Shared.Classes.ThreadManager, String, Threading.ThreadPriority) Initiates a thread and adds it to a thread pool
Initialise() Initialises all ThreadManager objects Called once at startup
Finalise() Finalises all ThreadManager objects Called once at finish
Cancel(String) Cancel's a running thread
CancelAll(Int32) Cancel's all threads, requesting that they close
UpdatePriority(Threading.ThreadPriority) Set's the priority for all threads
ThreadStart(Shared.Classes.ThreadManager, String, Threading.ThreadPriority, Boolean) Starts a new thread
thread_ThreadFinishing(Object, Shared.ThreadManagerEventArgs) Event method to remove from list of threads
RaiseThreadQueueRemoveItem(Shared.Classes.ThreadManager) Raises the thread queue remove item event
CancelThread(Int32, Boolean) Indicates that the thread should cancel
Abort() Indicates the thread should abort
CancelChildren() Request to cancel all child threads
ToString() Returns a string describing the Thread
ThreadRun(Object) Thread execution method
Run(Object) Method overridden in descendant class which will execute within the thread
HasCancelled() Determines wether the thread has been asked to cancel Descendant objects should check this and if set to true should quit doing whatever they are doing
IndicateNotHanging() Descendant threads can call this method to show they are still "active"
Ping() Long running threads will be pinged every 30 seconds to ensure they are not hanging Descendants should override and call IndicateNotHanging() method
UpdateThreadUsage(Int64, Int64, TimeSpan) Updates the thread usage, called internally
RaiseOnException(Exception) Raises an exception event
RaiseThreadStart(Shared.Classes.ThreadManager) Raises a thread start event
RaiseThreadFinished(Shared.Classes.ThreadManager) Raises a thread finish event
RaiseThreadCancelRequested(Shared.Classes.ThreadManager) Event raised to indicate Thread has been requested to cancel
RaiseThreadCancelChildrenRequested(Shared.Classes.ThreadManager) Event raised to indicate all child threads have been requested to cancel
RaiseThreadForcedToClose(Shared.Classes.ThreadManager) Event raised when a thread is forced to close
RaiseThreadAbortForced(Shared.Classes.ThreadManager) Indicates a thread was being forced to Abort
RaiseThreadCpuChanged() Raises an event to indicate the % of cpu usage for a thread has chenged by a specific amount
GetThreadMemoryUsage() Get's the memory usage for the thread
GetThreadTimes(TimeSpan) Retrieves the Total Thread Time

Fields

_cpuUsage CPU Usage object
_threadList List of managed threads
_threadPool Thread pool to hold threads if MaximumRunningThreads is exceeded
_abortPool List of threads to abort
_countOfThreads Number of managed threads
_lockObject Object used for exclusive locking
_threadManager Thread which manages the other threads in the list
_threadAbortManager Thread to abort other threads
_threadCacheManager Cache Manager for all Threads
_globalCancelRequested Indicates all threadshave been requested to cancel
_maximumRunningThreads Maximum number of threads that can be run
_allowThreadsToPool Determines wether a thread pool is available or not
_maximumThreadPoolSize Maximum number of waiting threads in the thread pool
_threadHangTimeoutMinutes Total number of minutes to wait before determining a thread is hanging if it fails to communicate
_checkForHangingThreads Indicates wether checks for hanging threads (those that do not communicate in a timely fashion) are made, or not
_threadCPUChangeNotification Event raised if thread cpu usage changes by this amount
_monitorCPUUsage Determines wether cpu usage is monitored for this thread
_prevThreadTotal Previous Thread Time (Kernal/User)
_thread Actual thread object
_delayStart Indicates wether the thread should delay starting in milliseconds
_cancel Indicates that the thread should cancel
_unresponsive flag to indicate wether the thread was unresponsive or not
_cancelTimeoutMilliseconds Number of milliseconds to wait for time out
_cancelRequested Date/time cancel thread requested
_parameters Thread parameters
_markedForRemoval Indicates the thread is marked for removal from the collection
_lastCommunication DateTime of last communication from the thread, it's upto the thread to update this value and it *could* be used to determine that the thread has timed out
_lastRun Date/Time thread last executed
_parentThread Parent Thread, if set
_childThreads All child threads