Chapter 16. Threading

A C# application runs in one or more threads that effectively execute in parallel within the same application. Here is a simple multithreaded application:

 using System; using System.Threading; class ThreadTest {   static void Main( ) {   Thread t = new Thread(new ThreadStart(Go));     t.Start( );     Go( );   }   static void Go( ) {     for (char c='a'; c<='z'; c++ )       Console.Write(c);   } } 

In this example, a new thread object is constructed by passing it a ThreadStart delegate that wraps the method that specifies where to start execution for that thread. Then start the thread and call Go , so two separate threads are running Go in parallel. However, there's one problem. Both threads share a common resource: the console. If you run ThreadTest , you receive output something like this:

 abcdabcdefghijklmnopqrsefghijklmnopqrstuvwxyztuvwxyz 


C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net