Monday 11 August 2014

Thread in C#

Threads
Thread use the maximum performance and of the above processors (advantage of parallel processing) we have to use the threads with our C# applications.
background thread
This thread remains runing even the main thread finishes
it's job, to finalize all the threads when main thread dies you have to set the IsBackground property to true.

class Threadings
    {
        bool _loop = true;
        public void method()
        {
            Threadings th = new Threadings();
            Thread t1 = new Thread(somethread);
            t1.Start(th);
            Thread.Sleep(2000);
            th._loop = false;
            Console.WriteLine("value set to false");

        }
        private static void somethread(object o1)
        {
            Threadings t = (Threadings)o1;
            Console.WriteLine("Loop starts");
            while (t._loop)
            { }
            Console.WriteLine("loop stop");
        }

    }

output :



No comments:

Post a Comment