Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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...
 
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 6-Mar-12 2:01am
Edited 6-Mar-12 2:10am
v2
Comments
Pete O'Hanlon at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Parampreet.CCIT at 7-Mar-12 5:00am
   
how ThreadProc method pass the value to the ResultCallback method

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 6 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100