Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-12-2008
Member
 
Join Date: Aug 2008
Posts: 88
Threading Unleashed

Creating and executing a thread.
Thread synchronization: To allow threads to cooperate and compete for resources, it is necessary to provide mechanisms for synchronization and communication.
Interaction between threads: The program begins by creating an object of type Alpha (oAlpha) and a thread (oThread) that references the Beta method of the Alpha class. The thread is then started. The IsAlive property of the thread allows the program to wait until the thread is initialized (created, allocated, and so on). The main thread is accessed through Thread, and the Sleep method tells the thread to give up its time slice and stop executing for a certain amount of milliseconds. The oThread is then stopped and joined. Joining a thread makes the main thread wait for it to die or for a specified time to expire (for more details, see Thread.Join Method). Finally, the program attempts to restart oThread, but fails because a thread cannot be restarted after it is stopped (aborted).


Code:
using System;
using System.Threading;

public class Alpha
{

    // This method that will be called when the thread is started
    public void Beta()
    {
        while (true)
        {
            Console.WriteLine("Alpha.Beta is running in its own thread.");
        }
    }
};

public class Simple
{
        public static int Main()
        {
            Console.WriteLine("Thread Start/Stop/Join Sample");
            Alpha oAlpha = new Alpha();
            // Create the thread object, passing in the Alpha.Beta method
            // via a ThreadStart delegate. This does not start the thread.
            Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
            // Start the thread
            oThread.Start();
            // Spin for a while waiting for the started thread to become
            // alive:
            while (!oThread.IsAlive);
            // Put the Main thread to sleep for 1 millisecond to allow oThread
            // to do some work:
            Thread.Sleep(1);
            // Request that oThread be stopped
            oThread.Abort();
            // Wait until oThread finishes. Join also has overloads
            // that take a millisecond interval or a TimeSpan object.
            oThread.Join();
            Console.WriteLine();
            Console.WriteLine("Alpha.Beta has finished");

        try
            {
                Console.WriteLine("Try to restart the Alpha.Beta thread");
                oThread.Start();
            }
        catch (ThreadStateException)
            {
                Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
                Console.WriteLine("Expected since aborted threads cannot be restarted.");
            }
         return 0;
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Threading Unleashed"
Thread Thread Starter Forum Replies Last Post
Threading in c# .net Athreya Software Development 3 12-01-2011 03:29 PM
What is Hyper threading Ameeryan Overclocking & Computer Modification 2 06-10-2009 04:03 PM
What is Threading in C# Programming ? HAKAN Software Development 2 31-03-2009 11:18 AM
What is threading in JAVA? Arsenio Software Development 3 02-03-2009 08:29 PM
VB.NET threading question AmolP Software Development 3 14-02-2009 06:03 PM


All times are GMT +5.5. The time now is 12:46 AM.