Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a program which is given in MCTS 70-536 exam....but not able to understand its working...So i need support to learn this...

C#
using System;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Multiply m = new Multiply("Hello, world!", 13, new ResultDelegate(ResultCallback));

            Thread t = new Thread(new ThreadStart(m.ThreadProc));
            t.Start();
            Console.WriteLine("Main thread does some work, then waits.");
            t.Join();
            Console.WriteLine("Thread completed.");

            Console.ReadLine();
        }
        public static void ResultCallback(int retValue)
        {
            Console.WriteLine("Returned value: {0}", retValue);
        }
    }

    public delegate void ResultDelegate(int value);

    public class Multiply
    {
        private string greeting;
        private int value;

        private ResultDelegate callback;

        public Multiply(string _greeting, int _value, ResultDelegate _callback)
        {
            greeting = _greeting;
            value = _value;
            callback = _callback;
        }

        public void ThreadProc()
        {
            Console.WriteLine(greeting);
            if (callback != null)
            {
                callback(value * 2);
            }
        }
    }
}
Posted
Updated 6-Mar-12 1:10am
v2
Comments
Pete O'Hanlon 6-Mar-12 7:11am    
What bit don't you understand? What was the source for this program? Did you read it in a book? If so, was there no explanation alongside it?

Multiply m = new Multiply("Hello, world!", 13, new ResultDelegate(ResultCallback));

Follwing things are happing in this statement
a. Value is passed to the Multiply class which is used as a input to the Thread.
b. New Delegate is created which will call ResultCallback from the Thread, which in turn is returning value back to the Program class.

This is the main essence of this program
 
Share this answer
 
Comments
Parampreet.CCIT 7-Mar-12 5:00am    
how ThreadProc method pass the value to the ResultCallback method
It's illustratinbg how a thread can set a value that can be used outside of the thread (especially useful for checking the results of a thread, as in the example above).

If you can't analyze existing code, your career as a programmer will be short.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900