Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

106Part II HANDLING DATA

Placing and releasing a lock on an object is a resource-intensive activity and adds to the overhead cost of the application.

Another critical problem that occurs while synchronizing objects is deadlocking. Consider a situation in which two different objects are locked by two different threads. Now, each thread wants to access the objects

locked by the other. If thread1 wants to access object1 that is locked by thread2, thread1 enters the sleep mode for the time thread2 releases the lock on object1. However, this never happens, because thread2 itself enters the sleep mode while trying to access object2, which is held by thread1. Both the threads go to sleep while waiting for the other to release its lock. This situation results in a deadlock because even the operating system cannot release the lock on the objects. The locks on object1 and object2 can only be released by thread2 and thread1, respectively. Therefore, the application hangs.

TIP

Despite the problems that are discussed here, locks are required on objects in a multithreaded environment.Therefore, you need to be careful while synchronizing the objects in applications.

Summary

In this chapter, you learned that a thread is a basic unit of execution of a program. In other words, a thread is the smallest unit to which the operating system allocates processor time.Threads allow you to execute multiple applications simultaneously and make the execution of a complex application simple and less time consuming. Therefore, you can create threads for your application. A thread that you create is an instance of the Thread class.The Thread class is a .NET base class located in the System.Threading namespace. You can then create the object of the Thread class that represents a thread.

In addition to creating threads, you learned about performing operations on threads that include aborting, sleeping, suspending, and resuming threads. In this chapter, you also learned about various states and priorities of threads.The change in the state of a thread is a result of the action performed on the thread. The

THREADS

Chapter 6

107

 

 

 

thread priorities supported by C# are Highest, AboveNormal, Normal, BelowNormal, and Lowest. All of these priority levels are a part of an enumeration object known as ThreadPriorityEnumeration. Finally, you learned about synchronization, which ensures that a variable can be accessed by only one thread at a time. By preventing multiple threads from accessing a single variable, you can ensure bug-free execution of the threads on your system.

This page intentionally left blank

 

 

Y

 

L

 

F

 

M

 

A

 

E

 

T

 

 

Team-Fly®

PART IIIProfessional Project 1

This page intentionally left blank

Project 1

Creating a

Customer

Maintenance

Project

Project 1 Overview

Now that you have looked at the basics of C#, you can move on to developing projects by using C#. Beginning with this chapter, you will learn more about using C# and its features through projects. These projects will help you to get a better understanding of the features of C#, which will enable you to apply these features in real-life projects.

In this project, you will learn to create a customer maintenance system for CarKare, Inc. This involves creating a Windows application that contains Windows forms. In addition, you will learn to connect these Windows forms to an underlying database such that the Windows forms display data from the SQL database. Finally, you will learn to create crystal reports that provide you with an analysis of the customer data and the data of the job done by a worker in a month.

You will learn to create the Windows application throughout this project. In the next chapter, I will discuss the case study and design of the Windows application.

Chapter 7

Project

Case Study