Showing posts with label Threading. Show all posts
Showing posts with label Threading. Show all posts

Saturday, February 4, 2012

How a thread is created ?

First, create a new Thread Start delegate. The delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. Finally, call the Thread.Start method to run your method.


In Detail :


1. Create a System.Threading.Thread object.
2. Create the call back function
3. Starting the Thread




Thread newThread= new Thread(new ThreadStart (MyCallbackFunction));


                 /  \                                                      /  \
                   |         (1)                                            |          (2)




newThread.Start()


        /  \
          |        (3)







What is thread join?

Stops of prevents the calling thread until a called thread terminates, and side by side continuing to perform standard COM and SendMessage pumping.


Syntax of this method :


// create a new thread object from thread class
Thread threadToJoin  =  new Thread(threadmethod); 


//Thread have come to running state.
threadToJoin.Start();   


 // Wait for foreground thread to end.
threadToJoin.Join()
^
 |   ____ so this threadToJoin thread will wait, until other threads are terminated.


Example : Suppose you are cooking something, and in between you need something to finish cooking, so you send your servant to get that, so you will call your cooking thread .join to wait till the servant thread not finished/ servant does not come with required item, and when servant arrives with the item servant thread terminates and your thread can continue.
               
void threadmethod()
{
    //method body
}




Live

Your Ad Here