- •About the Authors
- •Contents at a Glance
- •Contents
- •Introduction
- •Goal of the Book
- •How to Use this Book
- •Introduction to the .NET Framework
- •Common Language Runtime (CLR)
- •Class Library
- •Assembly
- •Versioning
- •Exceptions
- •Threads
- •Delegates
- •Summary
- •Introduction to C#
- •Variables
- •Initializing Variables
- •Variable Modifiers
- •Variable Data Types
- •Types of Variables
- •Variable Scope
- •Types of Data Type Casting
- •Arrays
- •Strings
- •Initializing Strings
- •Working with Strings
- •Statements and Expressions
- •Types of Statements
- •Expressions
- •Summary
- •Classes
- •Declaring Classes
- •Inheritance
- •Constructors
- •Destructors
- •Methods
- •Declaring a Method
- •Calling a Method
- •Passing Parameters to Methods
- •Method Modifiers
- •Overloading a Method
- •Namespaces
- •Declaring Namespaces
- •Aliases
- •Structs
- •Enumerations
- •Interfaces
- •Writing, Compiling, and Executing
- •Writing a C# Program
- •Compiling a C# Program
- •Executing a C# Program
- •Summary
- •Arrays
- •Single-Dimensional Arrays
- •Multidimensional Arrays
- •Methods in Arrays
- •Collections
- •Creating Collections
- •Working with Collections
- •Indexers
- •Boxing and Unboxing
- •Preprocessor Directives
- •Summary
- •Attributes
- •Declaring Attributes
- •Attribute Class
- •Attribute Parameters
- •Default Attributes
- •Properties
- •Declaring Properties
- •Accessors
- •Types of Properties
- •Summary
- •Introduction to Threads
- •Creating Threads
- •Aborting Threads
- •Joining Threads
- •Suspending Threads
- •Making Threads Sleep
- •Thread States
- •Thread Priorities
- •Synchronization
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Primary and Foreign Keys
- •Referential Integrity
- •Normalization
- •Designing a Database
- •Low-Level Design
- •Construction
- •Integration and Testing
- •User Acceptance Testing
- •Implementation
- •Operations and Maintenance
- •Summary
- •Creating a New Project
- •Console Application
- •Windows Applications
- •Creating a Windows Application for the Customer Maintenance Project
- •Creating an Interface for Form1
- •Creating an Interface for WorkerForm
- •Creating an Interface for CustomerForm
- •Creating an Interface for ReportsForm
- •Creating an Interface for JobDetailsForm
- •Summary
- •Performing Validations
- •Identifying the Validation Mechanism
- •Using the ErrorProvider Control
- •Handling Exceptions
- •Using the try and catch Statements
- •Using the Debug and Trace Classes
- •Using the Debugging Features of Visual Studio .NET
- •Using the Task List
- •Summary
- •Creating Form1
- •Connecting WorkerForm to the Workers Table
- •Connecting CustomerForm to the tblCustomer Table
- •Connecting the JobDetails Form
- •to the tblJobDetails Table
- •Summary
- •Introduction to the Crystal Reports Designer Tool
- •Creating the Reports Form
- •Creating Crystal Reports
- •Creating the Windows Forms Viewer Control
- •Creating the Monthly Worker Report
- •Summary
- •Introduction to Deploying a Windows Application
- •Deployment Projects Available in Visual Studio .NET
- •Deployment Project Editors
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Populating the TreeView Control
- •Displaying Employee Codes in the TreeView Control
- •Event Handling
- •Displaying Employee Details in the ListView Control
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Adding the Programming Logic to the Application
- •Adding Code to the Form Load() Method
- •Adding Code to the OK Button
- •Adding Code to the Exit Button
- •Summary
- •The Created Event
- •Adding Code to the Created Event
- •Overview of XML
- •The XmlReader Class
- •The XmlWriter Class
- •Displaying Data in an XML Document
- •Displaying an Error Message in the Event Log
- •Displaying Event Entries from Event Viewer
- •Displaying Data from the Summary.xml Document in a Message Box
- •Summary
- •Airline Profile
- •Role of a Business Manager
- •Role of a Network Administrator
- •Role of a Line-of-Business Executive
- •Project Requirements
- •Creation and Deletion of User Accounts
- •Addition of Flight Details
- •Reservations
- •Cancellations
- •Query of Status
- •Confirmation of Tickets
- •Creation of Reports
- •Launch of Frequent Flier Programs
- •Summarizing the Tasks
- •Project Design
- •Database Design
- •Web Forms Design
- •Enabling Security with the Directory Structure
- •Summary
- •Getting Started with ASP.NET
- •Prerequisites for ASP.NET Applications
- •New Features in ASP.NET
- •Types of ASP.NET Applications
- •Exploring ASP.NET Web Applications
- •Introducing Web Forms
- •Web Form Server Controls
- •Configuring ASP.NET Applications
- •Configuring Security for ASP.NET Applications
- •Deploying ASP.NET Applications
- •Creating a Sample ASP.NET Application
- •Creating a New Project
- •Adding Controls to the Project
- •Coding the Application
- •Summary
- •Creating the Database Schema
- •Creating Database Tables
- •Managing Primary Keys and Relationships
- •Viewing the Database Schema
- •Designing Application Forms
- •Standardizing the Interface of the Application
- •Common Forms in the Application
- •Forms for Network Administrators
- •Forms for Business Managers
- •Forms for Line-of-Business Executives
- •Summary
- •The Default.aspx Form
- •The Logoff.aspx Form
- •The ManageUsers.aspx Form
- •The ManageDatabases.aspx Form
- •The ChangePassword.aspx Form
- •Restricting Access to Web Forms
- •The AddFl.aspx Form
- •The RequestID.aspx Form
- •The Reports.aspx Form
- •The FreqFl.aspx Form
- •Coding the Forms for LOB Executives
- •The CreateRes.aspx Form
- •The CancelRes.aspx Form
- •The QueryStat.aspx Form
- •The ConfirmRes.aspx Form
- •Summary
- •Designing the Form
- •The View New Flights Option
- •The View Ticket Status Option
- •The View Flight Status Option
- •The Confirm Reservation Option
- •Testing the Application
- •Summary
- •Locating Errors in Programs
- •Watch Window
- •Locals Window
- •Call Stack Window
- •Autos Window
- •Command Window
- •Testing the Application
- •Summary
- •Managing the Databases
- •Backing Up the SkyShark Airlines Databases
- •Exporting Data from Databases
- •Examining Database Logs
- •Scheduling Database Maintenance Tasks
- •Managing Internet Information Server
- •Configuring IIS Error Pages
- •Managing Web Server Log Files
- •Summary
- •Authentication Mechanisms
- •Securing a Web Site with IIS and ASP.NET
- •Configuring IIS Authentication
- •Configuring Authentication in ASP.NET
- •Securing SQL Server
- •Summary
- •Deployment Scenarios
- •Deployment Editors
- •Creating a Deployment Project
- •Adding the Output of SkySharkDeploy to the Deployment Project
- •Deploying the Project to a Web Server on Another Computer
- •Summary
- •Organization Profile
- •Project Requirements
- •Querying for Information about All Books
- •Querying for Information about Books Based on Criteria
- •Ordering a Book on the Web Site
- •Project Design
- •Database Design
- •Database Schema
- •Web Forms Design
- •Flowcharts for the Web Forms Modules
- •Summary
- •Introduction to ASP.NET Web Services
- •Web Service Architecture
- •Working of a Web Service
- •Technologies Used in Web Services
- •XML in a Web Service
- •WSDL in a Web Service
- •SOAP in a Web Service
- •UDDI in a Web Service
- •Web Services in the .NET Framework
- •The Default Code Generated for a Web Service
- •Testing the SampleWebService Web Service
- •Summary
- •Creating the SearchAll() Web Method
- •Creating the SrchISBN() Web Method
- •Creating the AcceptDetails() Web Method
- •Creating the GenerateOrder() Web Method
- •Testing the Web Service
- •Securing a Web Service
- •Summary
- •Creating the Web Forms for the Bookers Paradise Web Site
- •Adding Code to the Web Forms
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Overview of Mobile Applications
- •The Microsoft Mobile Internet Toolkit
- •Overview of WAP
- •The WAP Architecture
- •Overview of WML
- •The Mobile Web Form
- •The Design of the MobileTimeRetriever Application
- •Creating the Interface for the Mobile Web Forms
- •Adding Code to the MobileTimeRetriever Application
- •Summary
- •Creating the Forms Required for the MobileCallStatus Application
- •Creating the frmLogon Form
- •Creating the frmSelectOption Form
- •Creating the frmPending Form
- •Creating the frmUnattended Form
- •Adding Code to the Submit Button in the frmLogon Form
- •Adding Code to the Query Button in the frmSelectOption Form
- •Adding Code to the Mark checked as complete Button in the frmPending Form
- •Adding Code to the Back Button in the frmPending Form
- •Adding Code to the Accept checked call(s) Button in the frmUnattended Form
- •Adding Code to the Back Button in the frmUnattended Form
- •Summary
- •What Is COM?
- •Windows DNA
- •Microsoft Transaction Server (MTS)
- •.NET Interoperability
- •COM Interoperability
- •Messaging
- •Benefits of Message Queues
- •Limitations
- •Key Messaging Terms
- •Summary
- •Pointers
- •Declaring Pointers
- •Types of Code
- •Implementing Pointers
- •Using Pointers with Managed Code
- •Working with Pointers
- •Compiling Unsafe Code
- •Summary
- •Introduction to the Languages of Visual Studio .NET
- •Visual C# .NET
- •Visual Basic .NET
- •Visual C++ .NET
- •Overview of Visual Basic .NET
- •Abstraction
- •Encapsulation
- •Inheritance
- •Polymorphism
- •Components of Visual Basic .NET
- •Variables
- •Constants
- •Operators
- •Arrays
- •Collections
- •Procedures
- •Arguments
- •Functions
- •Adding Code to the Submit Button
- •Adding Code to the Exit Button
- •Summary
- •Introduction to Visual Studio .NET IDE
- •Menu Bar
- •Toolbars
- •Visual Studio .NET IDE Windows
- •Toolbox
- •The Task List Window
- •Managing Windows
- •Customizing Visual Studio .NET IDE
- •The Options Dialog Box
- •The Customize Dialog Box
- •Summary
- •Index
98 |
Part II |
HANDLING DATA |
|
|
|
|
|
||
|
When you call the Abort() method of the Thread class, the method throws the |
|||
|
ThreadAbortException exception. In addition to the base class that is used to |
|||
|
handle threads, C# provides base classes to generate exceptions. The Thread- |
|||
|
AbortException is an exception of the ThreadAbortException class. C# provides |
|||
|
you with no mechanism to handle this exception. In other words, if you try to |
|||
|
abort the thread that is being executed inside the try block, the C# compiler first |
|||
|
executes any associated finally blocks before aborting the thread. |
|||
|
As you have seen, .NET provides you with a mechanism for safer killing of |
|||
|
threads compared with the earlier environments that killed the targeted thread |
|||
|
instantly. Having learned about creating and aborting threads, the next section |
|||
|
|
|
|
Y |
|
will continue the discussion about working with threads.It will discuss the Join() |
|||
|
method that allows you to wait for a threadLto finish execution or to be killed by |
|||
|
the Abort() method. |
F |
||
|
Joining Threads |
M |
|
|
|
A |
|
||
|
|
|
|
|
|
C# allows you to wait for a thread to terminate before the C# compiler proceeds |
|||
|
|
|
E |
|
|
with the execution of the other thread. To do so, the Thread class contains a Join() |
|||
|
method, which takesTthe following syntax: |
|
||
Thread1.Join();
The previous statement calls the Join() method of the Thread class to wait for Thread1 to terminate. If you do not know the time the thread takes to terminate, you can also specify the maximum time for which you want the C# compiler to wait before proceeding with the execution of the next thread. If the maximum time limit is not specified, the compiler waits for the thread to terminate on its own.
The Join() method is often used with the Abort() method. As explained earlier, when you call the Abort() method, the thread is not terminated instantly if it is in the middle of the try block. This implies that you need to wait for the finally statements to be executed before the thread terminates. However, you might not know the time the C# compiler takes to execute the finally block, and you are not ready to wait for a long period of time. Therefore, you can call the Abort() method followed by the Join() method to terminate the thread.
Team-Fly®
THREADS |
Chapter 6 |
99 |
|
|
|
You have learned how to abort and join a thread. In some cases, you might only need to stop or suspend the execution of a thread for a specified time. The following section discusses suspending threads.
Suspending Threads
You have learned about aborting threads. When a thread is aborted, you cannot resume the execution of the thread. However, when you suspend a thread, you can resume its execution whenever required. To suspend the execution of a thread, you use the Suspend() method. The Suspend() method is another method of the Thread class and does not take any parameters. The syntax of the Suspend() method is:
Thread1.Suspend();
The Suspend() method does not kill the thread permanently. It just stops the execution of the thread until it is resumed. Therefore, when you need to restart the execution of a thread, you can call another method of the Thread class, the Resume() method. The Resume() method starts executing the thread from the point at which the execution was suspended. The syntax of the Resume() method is shown in the following code.
Thread1.Resume();
TIP
You can only resume the execution of a suspended thread.
Similar to the Abort() method, the Suspend() method does not instantly stop the execution of the targeted thread. It waits for the thread to reach a safe point before suspending it.
You can also call the Suspend() and Resume() methods from an executing thread. Therefore, a thread can call the Suspend() method to suspend itself or another thread.
For example, if thread1 suspends itself, another thread needs to call the Resume() method to restart its execution. However, if thread1 suspends another thread,such
100 |
Part II |
HANDLING DATA |
|
|
|
as thread2, the execution of thread2 is not resumed until thread1 calls the
Resume() method on thread2.
In addition to the Suspend() method, you can block the execution of a thread by calling the Sleep() method of the Thread class.
Making Threads Sleep
The Thread class contains another method, called the Sleep() method, to stop the execution of a thread for a specified time. You can specify the time for which you want to stop the execution of a thread by passing the time as a parameter to the Sleep() method. The time is specified in milliseconds. Consider the following example that puts the thread to sleep for 2 seconds.
Thread.Sleep(2000);
As you can see in the previous code, the Sleep() method is called by the class itself and not the instance of the class.
You may wonder how the Sleep() method is different from the Suspend() method. Both of these methods are used to stop the execution of a thread for some time. You have seen that the Suspend() method does not instantly stop the execution of the thread. It waits for the thread to reach a safe point before stopping its execution. However, if you need to block the execution of a thread immediately, you can do this by calling the Sleep() method instead of the Suspend() method.
Figure 6-1 shows the difference between the Sleep() and Suspend() methods.
FIGURE 6-1 Differences between the Suspend() and Sleep() methods
THREADS |
Chapter 6 |
|
101 |
|
|||
|
|
|
|
Until now, you have learned about various methods that can be used with threads. To understand more about these operations, here is a simple thread with operations performed on it.
using System;
using System.Threading; class Class1
{
public void Method1()
{
Console.WriteLine(“Method1 is the starting point of execution of the thread”);
}
public static void Main()
{
Class1 newClass = new Class1();
Thread Thread1 = new Thread(new ThreadStart(newClass.Method1));
Thread1.Name = “Sample Thread”;
Thread1.Start ();
Console.WriteLine (“The execution of Sample Thread has started.”);
Thread1.Abort();
}
}
The previous code imports the System and System.Threading namespaces in your program code. The code then creates a class with the name Class1 and declares a method Method1 in the class.This method is specified as the starting point of execution of a thread named Sample Thread, which is an instance of the Thread class. Next, an instance of the ThreadStart delegate is created that takes Method1 as the parameter. The instance of the Thread class is used to call the methods of the Thread class that perform operations on Sample Thread.
The output of the previous code is shown in Figure 6-2.
