Monday 2 January 2012

Threading and multi threading

Threading
Most developers begin their programming training by writing linear programs in
which the computer executes a single line of code, waits for processing to complete,
and continues to the next line of code. Such single-threaded programming has serious
limitations, however. The program might be unresponsive to the user while processing
a request. If the application is waiting for a network resource to respond, it cannot
do anything else. If the computer has multiple processors, only one processor is used
at a time, limiting performance.

Multiple Threads
Though multithreading can be an incredibly complex topic, many uses are very simple.
In fact, running a method in a background thread is as easy as adding the System
.Threading namespace to your application and calling ThreadPool.QueueUserWorkItem.
This lesson provides an overview of threading and shows you how to start methods in
background threads.

// C#
static void Main(string[] args)
{
// Queue the task.
ThreadPool.QueueUserWorkItem(ThreadProc);
Console.WriteLine("Main thread does some work, then sleeps.");
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
}
// This thread procedure performs the task.
static void ThreadProc(Object stateInfo)
{
Console.WriteLine("Hello from the thread pool.");
}





Start Thread and end thread


' VB
Sub Main()
' Create the thread object, passing in the
' DoWork method using a ThreadStart delegate.
Dim DoWorkThread As New Thread(New ThreadStart(AddressOf DoWork))
280 Chapter 7 Threading
' Start the thread.
DoWorkThread.Start()
' Wait one second to allow the thread to begin to run
Thread.Sleep(1000)
' Abort the thread
DoWorkThread.Abort()
Console.WriteLine("The Main() thread is ending.")
Thread.Sleep(6000)
End Sub
Public Sub DoWork()
Console.WriteLine("DoWork is running on another thread.")
Try
Thread.Sleep(5000)
Catch ex As ThreadAbortException
Console.WriteLine("DoWork was aborted.")
Finally
Console.WriteLine("Use finally to close all open resources.")
End Try
Console.WriteLine("DoWork has ended.")
End Sub
// C#
static void Main(string[] args)
{
// Create the thread object, passing in the
// DoWork method using a ThreadStart delegate.
Thread DoWorkThread = new Thread(new ThreadStart(DoWork));
// Start the thread.
DoWorkThread.Start();
// Wait one second to allow the thread to begin to run
Thread.Sleep(1000);
// Abort the thread
DoWorkThread.Abort();
Console.WriteLine("The Main() thread is ending.");
Thread.Sleep(6000);
}
public static void DoWork()
{
Console.WriteLine("DoWork is running on another thread.");
try
{
Thread.Sleep(5000);
}
Lesson 2: Managing Threads 281
catch (ThreadAbortException ex)
{
Console.WriteLine("DoWork was aborted.");
}
finally
{
Console.WriteLine("Use finally to close all open resources.");
}
Console.WriteLine("DoWork has ended.");
}
This program outputs the following:
DoWork is running on another thread.
DoWork was aborted.
Use finally to close all open resources.
The Main() thread is ending.

No comments:

Post a Comment