Introduction

 < Day Day Up > 



Multithreading is what enables complex applications to appear to be performing numerous tasks at the same time. They may respond to user events, while at the same time accessing network resources, or the file system. Such concurrent applications are written in different ways depending on the platform and the operating system, giving varying control over this process. Visual Basic 6, for instance, gave you little or no control, and it would implement threading behind the scenes, so that when an event occurred, it would execute the appropriate handling code within a particular threading model, but the application programmer never needed to concern themself with it. Visual C++ developers had access to the full complexity of the Windows threading and process model, but with great power comes great responsibility: C++ programmers could easily create multithreaded monsters, and had to learn and use a range of complex tricks to ensure that the threads were kept under control.

The .NET Framework's managed coding environment has made available a full and powerful threading model that allows you to control exactly what runs in a thread, when the thread exits, and how much data it should have access to. However, just as the Common Language Runtime has taken responsibility for memory management out of the hands of programmers, it has also taken much of the responsibility for managing and cleaning up threads. So, in .NET we have a happy medium between the power of C++ and the simplicity of Visual Basic. That said, multithreaded applications introduce a whole range of programming problems that single-threaded programs never encounter.

This book will teach you how to take advantage of the threading capabilities provided by the .NET Framework, guiding you through the various features made available to you, while pointing out pitfalls for you to avoid.

When is threading used? Well, in fact, all programs execute in threads, so understanding how .NET and Windows execute threads will help you understand just what's going on inside your program at run time. Windows Forms applications use event-loop threads to handle user interface events. Separate forms execute on separate threads, so if you need to communicate between Windows Forms, you need to communicate between threads. ASP.NET pages execute inside the multi-threaded environment of IIS - separate requests for the same page may execute on different threads, and the same page may be executing on more than one thread simultaneously. When accessing shared resources from an ASP.NET page, you'll encounter threading issues.

As well as writing code that is executed in a multithreaded environment such as this, we often need to take control and actively create and control our own threads. Perhaps you need to create an application that never or rarely waits while processing some data, and is permanently available to respond to users and events. This can only happen if you build a multithreaded application. You can find many articles on the Web, and chapters in other books that tell you how to create a thread with the .NET Framework and how to perform some rudimentary operations; however, implementing the code is only half of the story. When you are using a multithreaded application, the type of operations that would normally block your application, such as file system operations, and so are ideal candidates for threading, are the kinds of operations that could produce synchronization or scalability issues, as more than one thread could be operating on the same file at the same time. This book, apart from teaching you how to create and manipulate threads, teaches you how to design your application so that you can avoid many of these issues by applying the appropriate kind of lock, and not blocking a thread while it waits for some other operation to complete.

Who Is This Book For?

This book is for C# developers who want to explore the full capabilities of the .NET platform. If you want to understand how C# code is executed inside the .NET Runtime, write code which is safe to execute in a multi-threaded system, and create and control threads in your own code, then this book will help you.

This book assumes you're already coding with C#, you're already familiar with the basic syntax, and you're regularly writing code that works. You should be familiar with your chosen development tools and know how to compile and run C# code.



 < Day Day Up > 



C# Threading Handbook
C# Threading Handbook
ISBN: 1861008295
EAN: 2147483647
Year: 2003
Pages: 74

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