12. Threading

A thread is an independent execution path.


Thread Examples
        static void TestClient()
        {
            Thread t = new Thread(MethodA);          
            t.Start();                               
            for (int i = 0; i < 1000; i++) Console.Write(++i);
        }
        static void MethodA()
        {
            for (int i = 0; i < 1000; i++) Console.Write(++i);
        }
        

        using System;
        using System.Threading;
        namespace ConsoleApp
        {
            public class Test
            {
                public void Display()
                {
                    while (true)
                    {
                        Console.WriteLine("Display meethod is running.");
                    }
                }
            };
            public static class ExampleThread
            {
                public static int TestClient()
                {
                    Console.WriteLine("Thread example");
                    Test oTest = new Test();
                    Thread oThread = new Thread(new ThreadStart(oTest.Display));
                    // Start the thread
                    oThread.Start();
                    while (!oThread.IsAlive) ;
                     Thread.Sleep(1);
                     oThread.Abort();
             
                    Console.WriteLine();
                    Console.WriteLine("Alpha.Beta has finished");
                    try
                    {
                        Console.WriteLine("restart the Display thread");
                        oThread.Start();
                    }
               
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message.ToString());
                    }
                    return 0;
                }
            }
        }