C# Essentials by Albahari Ben Drayton Peter Merrill Brad & Peter Drayton & Brad Merrill

C# Essentials by Albahari Ben Drayton Peter Merrill Brad & Peter Drayton & Brad Merrill

Author:Albahari, Ben, Drayton, Peter, Merrill, Brad & Peter Drayton & Brad Merrill [Ben Albahari]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / C#
ISBN: 9781449390839
Publisher: O'Reilly Media
Published: 2010-03-04T16:00:00+00:00


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. You then start the thread and call Go, so two separate threads are running Go in parallel. However, there’s a problem: both threads share a common resource—the console. If you run ThreadTest, you could get output like this:

abcdabcdefghijklmnopqrsefghijklmnopqrstuvwxyztuvwxyz



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.