Link to home
Start Free TrialLog in
Avatar of mandalorian4
mandalorian4

asked on

Challenging Multithread Question 500points

I am creating a Multithreaded C# Application.  The application will query a database for "jobs" to execute.  If it finds available jobs it will create a new "Job Object" (based on parameters supplied from the dataset returned from the query) and a  fresh worker thread for each job to be run.  This all works great.  I know that when the method (doSomeWork in the below example)  that the new thread calls exits the thread terminates.
Thread workthread = new Thread(new ThreadStart(doSomeWork));

What I need to accomplish is keeping track of all currently running threads.  When the thread is created it will increment an int variable, and decrement that variable when the thread exits.  I would also like to uniquely identify each thread.  So I can have, say a list box on a Winform showing currently executng threads.  So, essentially how do I monitor/track/label existing threads?

500 Points for some help.
ASKER CERTIFIED SOLUTION
Avatar of boblah
boblah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mandalorian4
mandalorian4

ASKER

boblah,

Thank you for the prompt response.  I will try to incorporate your solution shortly.  I noticed you used.

I am afraid I still do not understand how to identify a thread when multiple threads are created.  I see in my locals window a thread ID, but I do not see how to set or get the property.



Thank you.
AppDomain.GetCurrentThreadId() returns the unique id of the current thread.
You can assign a name to a thread.
To increment / decrement values threadsafe you can use the Interlocked.Decrement / Interlocked.Increment methods. (Evaluate the return value not the value in the variable)
BTW: You can not set the thread id. It's given by the system.
mandalorian4,

More details on creating a singleton

    /// <summary>
    /// Summary description for SingletonTest.
    /// This object works as a singleton
    /// for details of why this is the best way to do a proper, thread safe, lazy loaded singleton
    /// (one other way for full lazy loading, but slightly higher performance cost)
    /// see: http://www.yoda.arachsys.com/csharp/singleton.html
    /// sealed declaration is not strictly necessary, but might help optimisation
    /// </summary>
    public sealed class SingletonTest
    {
        public string CreateTime = "";

        #region Constructor etc
        private static readonly SingletonTest instance = new SingletonTest();
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static SingletonTest()
        {
        }
        public SingletonTest()
        {
            CreateTime = System.DateTime.Now.ToString();
        }
        /// <summary>
        /// the method could easily be converted to a property with only an accessor,
        /// with no impact on thread-safety or performance.
        /// </summary>
        /// <returns></returns>
        public static SingletonTest GetInstance()
        {
            return instance;
        }
        #endregion


    }