Working with Multiple Threads

This chapter, which winds up our C# survey, is on two important topics: multithreading and remoting . We'll tackle multithreading first, and turn to remotingthe process of sending types and objects across application and machine boundariesin the second half of the chapter.

As you might already know, a thread is a stream of execution in your code, and C# programs can support multiple threads. Using multithreading (also called free threading ), your code can be doing one thing with the user and another behind the scenes, such as working on some long, involved calculation. In this way, your code won't appear to freeze as that long calculation is performed. There are thousands of uses for threads, of courseyou might be working on the Internet while the user is working on something else, for example, or backing up data, or waiting to access a shared resource that other applications are using at the moment. Any time you want your application to be performing multiple tasks at the same time, think of multithreading as a possibility.

By default, each application is given a single thread, the main thread (to access the main thread, use the Thread class's CurrentThread property), but you can create others. Behind the scenes, however, creating new threads and letting them handle various tasks is a great idea. We're going to see how that works here. You create new threads using the Thread class. You can see the Thread class's significant public properties in Table 15.1, and its significant public methods in Table 15.2.

SHOP TALK : USER INTERFACE THREAD SAFETY

Here's an issue that you won't find discussed in enough depth in the C# documentationnot all threads are created equal. You should only work with user-interface elements like forms and controls ina .NET application's main thread (it's OK to use other threads to display text in console applications). In other words, user interface elements are not Thread Safe . The main thread has a lot of code to work with the user interface built into it; if you start working with user interface elements in another thread as well, there will be a conflict between threads sooner or later and your application is going to hang. This doesn't always happen every time you run the application, and in the past it's provided some non-repeatable bugs that took me a long time to find because the application code ran to dozens of pages (and it wasn't clear that a worker thread was calling main thread UI code). So, for example, if you want to undertake some complex calculation but want to let the user stop that calcualtion by clicking a button, you can handle the calculation in a new thread, and the button click in the main thread. Thread safety is one of those sticky issues that Microsoft has been working on for years with various programming models, such as the aprartment-threading model, which confines threads to "apartments," and which is not as powerful as true multithreading, We'll discuss more about thread safety when we synchorniczie our threads later in the chapter.


Table 15.1. Significant Public Properties of the Thread Class

PROPERTY

PURPOSE

CurrentCulture

Returns or sets the culture of the current thread.

CurrentThread

Returns the currently running thread.

IsAlive

True if the thread is active.

IsBackground

Returns or sets whether a thread is a background thread.

IsThreadPoolThread

Returns a value that indicates whether a thread belongs to a thread pool.

Name

Returns or sets the name of the thread.

Priority

Returns or sets the scheduling priority of a thread.

ThreadState

Returns the state of the current thread.

Table 15.2. Significant Public Methods of the Thread Class

METHOD

PURPOSE

Abort

Terminates a thread and raises a ThreadAbortException in the thread on which it is invoked.

GetDomain

Returns the domain in which the current thread is running.

GetDomainID

Returns a unique application domain identifier for the thread.

Interrupt

Interrupts a thread if it is in the WaitSleepJoin thread state.

Join

Returns only when a thread terminates.

MemoryBarrier

Synchronizes memory operations.

ResetAbort

Cancels an Abort requested for the thread.

Resume

Resumes operation in a thread that was suspended .

Sleep

Stops the current thread for the given number of milliseconds .

Start

Starts a thread's code executing.

Suspend

Suspends the thread until Resume is called.

We'll put the Thread class to work here, starting a few new threads and using them to demonstrate what you can do.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

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