using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Collections.Specialized; namespace Yaulw.Thread { /// /// Used to Manage/Launch Threads In an Application /// public static class TStarter { #region Public Delegates /// /// Thread Delegate with a single Object as a parameter /// /// object to be passed to Delegate public delegate void ParameterizedThreadMethod(object o); /// /// No Parameter Thread Delegate /// public delegate void ThreadMethod(); #endregion #region Private Static Members private static Dictionary _StartedNamedThreads = new Dictionary(); #endregion #region Public Statics /// /// Starts a Parameterized thread /// /// specifcy a method to invoke /// specify a parameter to pass to the method /// If you specify a ThreadName, it must be Unique to the Application /// ApartmentState /// Is Background Thread /// set thread priority /// a thread object if successful, null otherwise public static System.Threading.Thread StartParameterizedThread(ParameterizedThreadMethod method, object parameter, string threadName = "", ApartmentState Apartment = ApartmentState.MTA, bool IsBackground = false, ThreadPriority priority = ThreadPriority.Normal) { // If a threadName is defined, make sure that it is unique to the application if (!String.IsNullOrEmpty(threadName) && _StartedNamedThreads.ContainsKey(threadName)) return null; System.Threading.Thread thread = null; if (method != null && parameter != null) { thread = new System.Threading.Thread(new ParameterizedThreadStart(method)); if (!String.IsNullOrEmpty(threadName)) thread.Name = threadName; // Start the Thread thread.SetApartmentState(Apartment); thread.Priority = priority; thread.IsBackground = IsBackground; thread.Start(parameter); // Save the Thread in the Dictionary if (!String.IsNullOrEmpty(threadName)) _StartedNamedThreads[threadName] = thread; } return thread; } /// /// Starts a thread without Parameters /// /// specifcy a method to invoke /// If you specify a ThreadName, it must be Unique to the Application /// ApartmentState /// Is Background Thread /// set thread priority /// a thread object if successful, null otherwise public static System.Threading.Thread StartThread(ThreadMethod method, string threadName = "", ApartmentState Apartment = ApartmentState.MTA, bool IsBackground = false, ThreadPriority priority = ThreadPriority.Normal) { // If a threadName is defined, make sure that it is unique to the system if (!String.IsNullOrEmpty(threadName) && _StartedNamedThreads.ContainsKey(threadName)) return null; System.Threading.Thread thread = null; if (method != null) { thread = new System.Threading.Thread(new ThreadStart(method)); if (!String.IsNullOrEmpty(threadName)) thread.Name = threadName; // Start the Thread thread.SetApartmentState(Apartment); thread.Priority = priority; thread.IsBackground = IsBackground; thread.Start(); // Save the Thread in the Dictionary if (!String.IsNullOrEmpty(threadName)) _StartedNamedThreads[threadName] = thread; } return thread; } /// /// Retrieve all Named threads that were started thru here that are still running /// /// an array of running named threads, null if none public static System.Threading.Thread[] GetStartedNamedThreadsThatAreStillRunning() { List threads = new List(); List threadNamesThatAreNoLongerAlive = new List(); // Get Named Alive Threads foreach (System.Threading.Thread thread in _StartedNamedThreads.Values) { if (thread.IsAlive) threads.Add(thread); else threadNamesThatAreNoLongerAlive.Add(thread.Name); } // Perform Cleanup foreach (string threadName in threadNamesThatAreNoLongerAlive) { _StartedNamedThreads[threadName] = null; _StartedNamedThreads.Remove(threadName); } return threads.ToArray(); } #endregion } }