In the times when all the programming world was not writing database applications; there was an important concept “THREADING”.It may sound hard but actually not that hard.
Firstly a threat is an execution path able to run simultaneously with other threads.
This is how you create a basic thread
new Thread (TestMethod).Start();
this runs the TestMethodmethod in a new thread.
Thread t = new Thread (TestMethod);
t.Start();
is another way of starting a thread.You can name your thread with t.Name also.
You can stop a thread with Thread.Sleep();
class ThreadTest
{
static void Main()
{
Thread t = new Thread(Go);
Thread b = new Thread(No);
t.Start();
t.Join();
b.Start();
Console.Read();
}
static void Go()
{
for (int i = 0; i < 1000; i++)
Console.Write("X");
}
static void No()
{
for (int i = 0; i < 1000; i++)
Console.Write("Y");
}
}
thread.join(); waits for the thread to continue
A thread is said to be preempted when its execution is interrupted due to an external factor such as time-slicing. In most situations, a thread has no control over when and where it's preempted.Threads have their own stack but share heap
Passing Data to Threads ;If you want to pass just a single parameter to a thread you can use parameterizedThreadStart
public delegate void ParameterizedThreadStart (object obj);
static void Main()
{
Thread t = new Thread(Go);
Thread b = new Thread(No);
t.Start("Hi");
t.Join();
b.Start();
Console.Read();
}
static void Go(object o)
{
for (int i = 0; i < 1000; i++)
Console.Write(o.ToString());
}
static void No()
{
for (int i = 0; i < 1000; i++)
Console.Write("Y");
}
}
Ok this is the basics.I will continue with another post.
No comments:
Post a Comment