Beispiel #1
0
        static public void RunCountdown(ResultCallbackDelegate callback)
        {
            var countdown = new Thread(_RunCountdown);

            countdown.SetApartmentState(ApartmentState.STA);
            countdown.Start(callback);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //Create the ResultCallbackDelegate instance and to its constructor
            //pass the callback method name
            ResultCallbackDelegate resultCallbackDelegate = new ResultCallbackDelegate(ResultCallBackMethod);
            int Number = 10;
            //Creating the instance of NumberHelper class by passing the Number
            //the callback delegate instance
            NumberHelper obj = new NumberHelper(Number, resultCallbackDelegate);
            //Creating the Thread using ThreadStart delegate
            Thread T1 = new Thread(new ThreadStart(obj.CalculateSum));

            T1.Start();
            Console.Read();
        }
Beispiel #3
0
 //Initializing the private variables through constructor
 //So while creating the instance you need to pass the value for Number and callback delegate
 public NumberHelper(int Number, ResultCallbackDelegate resultCallbackDelagate)
 {
     _Number = Number;
     _resultCallbackDelegate = resultCallbackDelagate;
 }
Beispiel #4
0
 public NumberHelperWithRetrieve(int Number, ResultCallbackDelegate resultCallbackDelegate)
 {
     _Number = Number;
     _resultCallbackDelegate = resultCallbackDelegate;
 }
Beispiel #5
0
        static void Main(string[] args)
        {
            Thread t = Thread.CurrentThread;

            t.Name = "Main Thread";

            /*
             * This will work, but method 2 has a 10 second delay!
             * Method1();
             * Method2();
             * Method3();
             */

            Console.WriteLine(t.Name + " Started.");
            Thread t1 = new Thread(Method1)
            {
                Name = "Thread1"
            };

            Thread t2 = new Thread(Method2)
            {
                Name = "Thread2"
            };

            Thread t3 = new Thread(Method3)
            {
                Name = "Thread3"
            };

            Program obj = new Program();

            // This works, but is NOT type-safe!
            ParameterizedThreadStart pt1 = new ParameterizedThreadStart(Method4);
            Thread t4 = new Thread(pt1)
            {
                Name = "Thread4"
            };

            // We can make a type-safe thread by creating a class and
            // using its parameters
            NumberHelper nh1 = new NumberHelper(13);
            Thread       t5  = new Thread(new ThreadStart(nh1.Method5))
            {
                Name = "Thread5"
            };

            /* Or, could have done this
             * NumberHelper nh1 = new NumberHelper(13);
             * ThreadStart ts1 = new ThreadStart(nh1.Method5);
             * Thread t5 = new Thread(ts1)
             * {
             *      Name = "Thread5"
             * };
             */

            // Using a callback method
            ResultCallbackDelegate resultCallbackDelegate = new ResultCallbackDelegate(ResultCallBackMethod);
            // Pass callback delegate
            NumberHelperWithRetrieve nhr1 = new NumberHelperWithRetrieve(10, resultCallbackDelegate);
            // start thread
            Thread t6 = new Thread(new ThreadStart(nhr1.CalculateSum));


            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start(10);
            t5.Start();
            t6.Start();

            // Using join statement so main thread does not continue
            // until child threads have finished
            t1.Join();
            // Main thread is going to end before t3 because I have set the
            // timeout for 3 seconds and t3 take 10 seconds.
            t2.Join(3000);
            // Or wait till finished
            // t2.Join();
            t4.Join();
            t5.Join();
            t6.Join();

            if (t2.IsAlive)
            {
                Console.WriteLine(t3.Name + " is still doing work");
            }
            else
            {
                Console.WriteLine(t3.Name + " is finished");
            }
            Console.WriteLine("Main Thread ended!");
        }
Beispiel #6
0
 public MessageDisplayerWithRetrieve(int Wait, ResultCallbackDelegate resultCallbackDelegate)
 {
     _Wait = Wait;
     _resultCallbackDelegate = resultCallbackDelegate;
 }
Beispiel #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Main thread started.");

            // START OF LOCK DEMO
            Console.WriteLine("START OF LOCK DEMO");
            // Without locking!
            Console.WriteLine("Starting threads without locking");
            // Creating thread objects
            Thread t1 = new Thread(DisplayMessageNoLock);
            Thread t2 = new Thread(DisplayMessageNoLock);
            Thread t3 = new Thread(DisplayMessageNoLock);

            // Starting threads
            t1.Start();
            t2.Start();
            t3.Start();

            // Wait for threads to complete before continuing
            t1.Join();
            t2.Join();
            t3.Join();
            Console.WriteLine("Done with no lock demo");

            // With locking!
            Console.WriteLine("Now with locking");
            // (ThreadStart) needed due to overloaded method
            Thread t4 = new Thread((ThreadStart)DisplayMessage);
            Thread t5 = new Thread((ThreadStart)DisplayMessage);
            Thread t6 = new Thread((ThreadStart)DisplayMessage);

            // Start threads
            t4.Start();
            t5.Start();
            t6.Start();

            // Wait for threads to complete
            t4.Join();
            t5.Join();
            t6.Join();
            Console.WriteLine("Done with lock demo");

            // With locking and input parameter
            Console.WriteLine("Locking demo with input parameter of 500");
            ParameterizedThreadStart pt1 = new ParameterizedThreadStart(DisplayMessage);
            Thread t7 = new Thread(pt1);
            Thread t8 = new Thread(pt1);

            // Start threads
            t7.Start(500);
            t8.Start(500);

            // Wait for threads to complete
            t7.Join();
            t8.Join();
            Console.WriteLine("Done with Locking demo with input parameter of 500");

            // Type-safe
            Console.WriteLine("Locking demo with type-safe input parameter of 500");
            MessageDisplayer md1 = new MessageDisplayer(500);
            Thread           t9  = new Thread(md1.DisplayMessage);
            Thread           t10 = new Thread(md1.DisplayMessage);

            // Start threads
            t9.Start();
            t10.Start();

            // Wait for threads to complete
            t9.Join();
            t10.Join();
            Console.WriteLine("Done with Locking demo with type-safe input parameter of 500");

            // With type-safe with callback function
            Console.WriteLine("Locking demo with type-safe input parameter of 500 and callback function");
            ResultCallbackDelegate       resultCallback = new ResultCallbackDelegate(ResultCallbackMethod);
            MessageDisplayerWithRetrieve mdr1           = new MessageDisplayerWithRetrieve(500, resultCallback);
            Thread t11 = new Thread(mdr1.DisplayMessage);
            Thread t12 = new Thread(mdr1.DisplayMessage);


            t11.Start();
            t12.Start();

            // Wait for threads to complete
            t11.Join();
            t12.Join();
            Console.WriteLine("Done with Locking demo with type-safe input parameter of 500 and callback function");

            Console.WriteLine("END OF LOCK DEMO");
            // END OF LOCK DEMO

            // START OF MONITOR DEMO

            // Making and array of threads
            Thread[] Threads = new Thread[4];
            // Obstantiating object that I want to lock
            // See definition for use of Monitor methods
            DisplayNumbersMonitorClass dnmc1 = new DisplayNumbersMonitorClass();

            // Assign function to threads
            for (int i = 0; i < Threads.Length; i++)
            {
                Threads[i]      = new Thread(dnmc1.PrintNumbers);
                Threads[i].Name = "Child Thread " + i;
            }

            // Start each thread
            foreach (Thread t in Threads)
            {
                t.Start();
            }

            // Wait for each thread to complete
            foreach (Thread t in Threads)
            {
                t.Join();
            }
            Console.WriteLine("Main thread done.");
        }