Prelim -1 Contents at a Glance 4 Contents 6 About the Authors 20 About the Technical Reviewer 21 Acknowledgments 22 Introduction 23 Application Development 25 1-1. Create a Console Application from the Command Line 26 Problem 26 Solution 26 How It Works 27 The Code 27 Usage 28 1-2. Create a Windows-Based Application from the Command Line 29 Problem 29 Solution 29 How It Works 29 The Code 30 Usage 32 1-3. Create and Use a Code Module 33 Problem 33 Solution 33 How It Works 33 Usage 34 1-4. Create and Use a Code Library from the Command Line 35 Problem 35 Solution 35 How It Works 35 Usage 35 1-5. Access Command-Line Arguments 36 Problem 36 Solution 36 How It Works 36 The Code 37 Usage 37 1-6. Include Code Selectively at Build Time 38 Problem 38 Solution 38 How It Works 39 The Code 40 Usage 41 Notes 41 1-7. Access a Program Element That Has the Same Name As a Keyword 42 Problem 42 Solution 42 How It Works 42 The Code 42 1-8. Create and Manage Strongly Named Key Pairs 43 Problem 43 Solution 43 How It Works 43 1-9. Give an Assembly a Strong Name 45 Problem 45 Solution 45 How It Works 46 The Code 46 Usage 47 1-10. Verify That a Strongly Named Assembly Has Not Been Modified 47 Problem 47 Solution 47 How It Works 47 1-11. Delay-Sign an Assembly 48 Problem 48 Solution 49 How It Works 49 1-12. Sign an Assembly with an Authenticode Digital Signature 50 Problem 50 Solution 50 How It Works 51 Usage 51 1-13. Create and Trust a Test Software Publisher Certificate 52 Problem 52 Solution 52 How It Works 52 1-14. Manage the Global Assembly Cache 53 Problem 53 Solution 54 How It Works 54 1-15. Prevent People from Decompiling Your Code 54 Problem 54 Solution 54 How It Works 55 1-16. Manipulate the Appearance of the Console 55 Problem 55 Solution 56 How It Works 56 The Code 57 1-17. Create a Static Class 58 Problem 58 Solution 58 How It Works 58 The Code 58 1-18. Create an Anonymous Type 59 Problem 59 Solution 59 How It Works 59 The Code 60 1-19. Create an ExpandoObject Dynamic Type 61 Problem 61 Solution 61 How It Works 61 The Code 62 1-20. Define an Automatically Implemented Property 63 Problem 63 Solution 63 How It Works 63 The Code 63 1-21. Overload an Operator 65 Problem 65 Solution 65 How It Works 65 The Code 66 1-22. Define a Conversion Operator 68 Problem 68 Solution 68 How It Works 68 The Code 69 1-23. Handle an Event with an Anonymous Function 70 Problem 70 Solution 70 How It Works 70 The Code 71 1-24. Implement a Custom Indexer 72 Problem 72 Solution 72 How It Works 72 The Code 72 Data Manipulation 77 2-1. Manipulate the Contents of a String Efficiently 78 Problem 78 Solution 78 How It Works 78 The Code 79 2-2. Encode a String Using Alternate Character Encoding 80 Problem 80 Solution 80 How It Works 80 The Code 81 Usage 82 Notes 83 2-3. Convert Basic Value Types to Byte Arrays 83 Problem 83 Solution 83 How It Works 83 The Code 84 2-4. Base64 Encode Binary Data 85 Problem 85 Solution 86 How It Works 86 The Code 86 2-5. Validate Input Using Regular Expressions 89 Problem 89 Solution 90 How It Works 90 The Code 92 Usage 93 Notes 93 2-6. Use Compiled Regular Expressions 94 Problem 94 Solution 94 How It Works 94 The Code 95 2-7. Create Dates and Times from Strings 96 Problem 96 Solution 96 How It Works 97 The Code 97 2-8. Add, Subtract, and Compare Dates and Times 98 Problem 98 Solution 99 How It Works 99 The Code 100 2-9. Sort an Array or a Collection 101 Problem 101 Solution 101 How It Works 101 The Code 102 2-10. Copy a Collection to an Array 103 Problem 103 Solution 103 How It Works 104 The Code 104 2-11. Use a Strongly Typed Collection 105 Problem 105 Solution 105 How It Works 106 The Code 106 2-12. Create a Generic Type 107 Problem 107 Solution 107 How It Works 107 The Code 108 2-13. Store a Serializable Object to a File 111 Problem 111 Solution 111 How It Works 111 The Code 111 2-14. Serialize an Object Using JSON 114 Problem 114 Solution 115 How It Works 115 The Code 116 2-15. Read User Input from the Console 117 Problem 117 Solution 117 How It Works 117 The Code 118 2-16. Using Large Integer Values 120 Problem 120 Solution 120 How It Works 120 The Code 120 2-17. Select Collection or Array Elements 121 Problem 121 Solution 121 How It Works 121 The Code 122 2-18. Remove Duplicate Items from an Array or Collection 124 Problem 124 Solution 124 How It Works 124 The Code 124 Application Domains, Reflection, and Metadata 127 3-1. Create an Application Domain 128 Problem 128 Solution 128 How It Works 128 The Code 129 3-2. Create Types That Can Be Passed Across Application Domain Boundaries 130 Problem 130 Solution 130 How It Works 130 The Code 131 3-3. Avoid Loading Unnecessary Assemblies into Application Domains 133 Problem 133 Solution 133 How It Works 133 The Code 134 3-4. Create a Type That Cannot Cross Application Domain Boundaries 135 Problem 135 Solution 135 How It Works 135 3-5. Load an Assembly into the Current Application Domain 135 Problem 135 Solution 135 How It Works 136 The Code 137 3-6. Execute an Assembly in a Different Application Domain 138 Problem 138 Solution 138 How It Works 138 The Code 139 Usage 140 3-7. Instantiate a Type in a Different Application Domain 140 Problem 140 Solution 140 How It Works 140 The Code 141 3-8. Pass Data Between Application Domains 145 Problem 145 Solution 145 How It Works 145 The Code 146 3-9. Unload Assemblies and Application Domains 148 Problem 148 Solution 148 How It Works 148 The Code 148 3-10. Retrieve Type Information 149 Problem 149 Solution 149 How It Works 149 The Code 150 3-11. Test an Object’s Type 151 Problem 151 Solution 151 How It Works 151 The Code 152 3-12. Instantiate an Object Using Reflection 153 Problem 153 Solution 154 How It Works 154 The Code 154 3-13. Create a Custom Attribute 157 Problem 157 Solution 157 How It Works 157 The Code 158 Usage 159 3-14. Inspect the Attributes of a Program Element Using Reflection 160 Problem 160 Solution 160 How It Works 160 The Code 161 3-15. Programmatically Discover the Members of a Type 161 Problem 161 Solution 162 How It Works 162 The Code 162 3-16. Invoke a Type Member Using Reflection 164 Problem 164 Solution 164 How It Works 164 The Code 164 3-17. Dynamically Invoke a Type Member 166 Problem 166 Solution 166 How It Works 166 The Code 166 3-18. Create a Custom Dynamic Type 167 Problem 167 Solution 167 How It Works 167 The Code 169 Threads, Processes, and Synchronization 173 4-1. Execute a Method Using the Thread Pool 175 Problem 175 Solution 175 How It Works 175 The Code 175 Notes 177 4-2. Execute a Method Asynchronously 178 Problem 178 Solution 178 How It Works 179 The Code 180 4-3. Execute a Method Periodically 187 Problem 187 Solution 188 How It Works 188 The Code 188 4-4. Execute a Method at a Specific Time 190 Problem 190 Solution 190 How It Works 190 The Code 190 4-5. Execute a Method by Signaling a WaitHandle Object 191 Problem 191 Solution 191 How It Works 191 The Code 192 4-6. Execute a Method Using a New Thread 193 Problem 193 Solution 193 How It Works 193 The Code 194 4-7. Synchronize the Execution of Multiple Threads Using a Monitor 196 Problem 196 Solution 196 How It Works 196 The Code 198 4-8. Synchronize the Execution of Multiple Threads Using an Event 202 Problem 202 Solution 202 How It Works 202 The Code 203 4-9. Synchronize the Execution of Multiple Threads Using a Mutex 206 Problem 206 Solution 206 How It Works 206 The Code 206 4-10. Synchronize the Execution of Multiple Threads Using a Semaphore 209 Problem 209 Solution 209 How It Works 209 The Code 209 4-11. Synchronize Access to a Shared Data Value 211 Problem 211 Solution 211 How It Works 211 The Code 212 4-12. Know When a Thread Finishes 214 Problem 214 Solution 214 How It Works 214 The Code 214 4-13. Terminate the Execution of a Thread 215 Problem 215 Solution 216 How It Works 216 The Code 216 4-14. Create a Thread-Safe Collection Instance 217 Problem 217 Solution 218 How It Works 218 The Code 219 4-15. Start a New Process 219 Problem 219 Solution 220 How It Works 220 The Code 221 4-16. Terminate a Process 223 Problem 223 Solution 223 How It Works 223 The Code 224 4-17. Ensure That Only One Instance of an Application Can Execute Concurrently 225 Problem 225 Solution 225 How It Works 225 The Code 226 Files, Directories, and I/O 229 5-1. Retrieve Information About a File, Directory, or Drive 230 Problem 230 Solution 230 How It Works 230 The Code 232 Usage 234 5-2. Set File and Directory Attributes 235 Problem 235 Solution 235 How It Works 235 The Code 236 5-3. Copy, Move, or Delete a File or Directory 237 Problem 237 Solution 237 How It Works 237 The Code 238 5-4. Calculate the Size of a Directory 240 Problem 240 Solution 240 How It Works 240 The Code 240 5-5. Retrieve Version Information for a File 241 Problem 241 Solution 241 How It Works 241 The Code 242 Usage 242 5-6. Show a Just-in-Time Directory Tree in the TreeView Control 243 Problem 243 Solution 243 How It Works 244 The Code 244 5-7. Read and Write a Text File 246 Problem 246 Solution 246 How It Works 246 The Code 247 5-8. Read and Write a Binary File 248 Problem 248 Solution 249 How It Works 249 The Code 249 5-9. Read a File Asynchronously 250 Problem 250 Solution 250 How It Works 251 The Code 251 Usage 252 5-10. Find Files That Match a Wildcard Expression 254 Problem 254 Solution 254 How It Works 254 The Code 254 5-11. Test Two Files for Equality 255 Problem 255 Solution 255 How It Works 256 The Code 256 5-12. Manipulate Strings Representing File Names 257 Problem 257 Solution 257 How It Works 257 5-13. Determine If a Path Is a Directory or a File 259 Problem 259 Solution 259 How It Works 259 The Code 259 5-14. Work with Relative Paths 260 Problem 260 Solution 260 How It Works 260 The Code 260 Usage 261 5-15. Create a Temporary File 262 Problem 262 Solution 262 How It Works 262 The Code 262 5-16. Get the Total Free Space on a Drive 263 Problem 263 Solution 263 How It Works 263 The Code 263 5-17. Show the Common File Dialog Boxes 265 Problem 265 Solution 265 How It Works 265 The Code 266 5-18. Use an Isolated Store 267 Problem 267 Solution 268 How It Works 268 The Code 268 5-19. Monitor the File System for Changes 270 Problem 270 Solution 270 How It Works 270 The Code 271 5-20. Access a COM Port 272 Problem 272 Solution 272 How It Works 272 The Code 272 5-21. Get a Random File Name 273 Problem 273 Solution 273 How It Works 273 The Code 274 5-22. Manipulate the Access Control List of a File or Directory 274 Problem 274 Solution 274 How It Works 275 The Code 275 5-23. Compress Data 277 Problem 277 Solution 277 How It Works 277 The Code 277 5-24. Log Data to a File 278 Problem 278 Solution 278 How It Works 279 The Code 279 5-25. Process a Log File 280 Problem 280 Solution 280 How It Works 280 The Code 280 5-26. Communicate Between Processes 281 Problem 281 Solution 281 How It Works 281 The Code 281 XML Processing 285 6-1. Show the Structure of an XML Document in a TreeView 285 Problem 285 Solution 286 How It Works 286 The Code 287 Usage 289 6-2. Insert Nodes in an XML Document 290 Problem 290 Solution 290 How It Works 290 The Code 291 6-3. Quickly Append Nodes in an XML Document 292 Problem 292 Solution 293 How It Works 293 The Code 293 6-4. Find Specific Elements by Name 295 Problem 295 Solution 295 How It Works 295 The Code 295 Notes 296 6-5. Get XML Nodes in a Specific XML Namespace 296 Problem 296 Solution 297 How It Works 297 The Code 297 6-6. Find Elements with an XPath Search 298 Problem 298 Solution 299 How It Works 299 The Code 299 Notes 300 6-7. Read and Write XML Without Loading an Entire Document into Memory 302 Problem 302 Solution 302 How It Works 302 The Code 303 6-8. Validate an XML Document Against a Schema 305 Problem 305 Solution 305 How It Works 305 The Code 307 6-9. Use XML Serialization with Custom Objects 310 Problem 310 Solution 310 How It Works 311 The Code 312 6-10. Create a Schema for a .NET Class 314 Problem 314 Solution 314 How It Works 314 Usage 314 6-11. Generate a Class from a Schema 315 Problem 315 Solution 315 How It Works 315 Usage 315 6-12. Perform an XSL Transform 316 Problem 316 Solution 316 How It Works 316 The Code 316 6-13. Load XML with LINQ 320 Problem 320 Solution 320 How It Works 320 The Code 320 6-14. Create a New XML Tree with LINQ 322 Problem 322 Solution 322 How It Works 322 The Code 323 6-15. Query XML with LINQ 324 Problem 324 Solution 325 How It Works 325 The Code 325 6-16. Modify an XML Tree with LINQ 327 The Problem 327 The Solution 327 How It Works 327 The Code 327 Windows Forms 331 7-1. Add a Control Programmatically 333 Problem 333 Solution 333 How It Works 333 The Code 333 7-2. Store Data with a Control 335 Problem 335 Solution 335 How It Works 336 The Code 336 7-3. Process All the Controls on a Form 338 Problem 338 Solution 338 How It Works 338 The Code 338 7-4. Track the Visible Forms in an Application 339 Problem 339 Solution 339 How It Works 339 The Code 340 Notes 342 7-5. Find All MDI Child Forms 343 Problem 343 Solution 343 How It Works 343 The Code 343 7-6. Save Configuration Settings for a Form 346 Problem 346 Solution 346 How It Works 346 The Code 347 7-7. Force a List Box to Scroll to the Most Recently Added Item 349 Problem 349 Solution 349 How It Works 349 The Code 349 7-8. Restrict a Text Box to Accept Only Specific Input 350 Problem 350 Solution 350 How It Works 350 The Code 351 Notes 353 7-9. Use an Autocomplete Combo Box or Text Box 353 Problem 353 Solution 353 How It Works 353 The Code 354 7-10. Sort a List View by Any Column 356 Problem 356 Solution 356 How It Works 356 The Code 356 7-11. Lay Out Controls Automatically 359 Problem 359 Solution 359 How It Works 359 7-12. Use Part of a Main Menu for a Context Menu 360 Problem 360 Solution 360 How It Works 360 The Code 360 7-13. Make a Multilingual Form 362 Problem 362 Solution 362 How It Works 362 The Code 364 7-14. Create a Form That Cannot Be Moved 365 Problem 365 Solution 365 How It Works 365 The Code 366 7-15. Make a Borderless Form Movable 367 Problem 367 Solution 367 How It Works 367 The Code 368 7-16. Create an Animated System Tray Icon 370 Problem 370 Solution 370 How It Works 370 The Code 370 7-17. Validate an Input Control 371 Problem 371 Solution 372 How It Works 372 The Code 372 7-18. Use a Drag-and-Drop Operation 374 Problem 374 Solution 374 How It Works 375 The Code 375 7-19. Update the User Interface in a Multithreaded Application 378 Problem 378 Solution 378 How It Works 378 The Code 379 7-20. Display a Web Page in a Windows-Based Application 380 Problem 380 Solution 380 How It Works 381 The Code 382 7-21. Display WPF Windows in a Windows Forms Application 384 Problem 384 Solution 384 How It Works 384 The Code 384 7-22. Display WPF Controls in Windows Forms 387 Problem 387 Solution 387 How It Works 387 The Code 388 Graphics, Multimedia, and Printing 393 8-1. Find All Installed Fonts 394 Problem 394 Solution 394 How It Works 394 The Code 394 8-2. Perform Hit Testing with Shapes 396 Problem 396 Solution 396 How It Works 396 The Code 397 8-3. Create an Irregularly Shaped Control 400 Problem 400 Solution 400 How It Works 400 The Code 401 8-4. Create a Movable Sprite 403 Problem 403 Solution 403 How It Works 403 The Code 403 8-5. Create a Scrollable Image 407 Problem 407 Solution 407 How It Works 407 The Code 407 8-6. Perform a Screen Capture 409 Problem 409 Solution 409 How It Works 409 The Code 409 8-7. Use Double Buffering to Increase Redraw Speed 410 Problem 410 Solution 410 How It Works 411 The Code 411 8-8. Show a Thumbnail for an Image 413 Problem 413 Solution 413 How It Works 413 The Code 413 8-9. Play a Simple Beep or System Sound 415 Problem 415 Solution 415 How It Works 415 The Code 415 8-10. Play a WAV File 416 Problem 416 Solution 416 How It Works 416 The Code 416 8-11. Play a Sound File 417 Problem 417 Solution 418 How It Works 418 The Code 418 8-12. Play a Video 419 Problem 419 Solution 419 How It Works 419 The Code 420 8-13. Retrieve Information About Installed Printers 422 Problem 422 Solution 422 How It Works 422 The Code 423 Usage 424 8-14. Print a Simple Document 425 Problem 425 Solution 425 How It Works 425 The Code 427 8-15. Print a Multipage Document 428 Problem 428 Solution 428 How It Works 429 The Code 429 8-16. Print Wrapped Text 432 Problem 432 Solution 432 How It Works 432 The Code 432 8-17. Show a Dynamic Print Preview 434 Problem 434 Solution 434 How It Works 434 The Code 435 8-18. Manage Print Jobs 436 Problem 436 Solution 436 How It Works 437 The Code 437 8-19. Perform Text-to-Speech 441 Problem 441 Solution 441 How It Works 442 The Code 442 8-20. Recognize Characters in an Image (OCR) 443 Problem 443 Solution 444 How It Works 444 The Code 445 Database Access 447 9-1. Connect to a Database 449 Problem 449 Solution 449 How It Works 449 The Code 450 9-2. Use Connection Pooling 452 Problem 452 Solution 452 How It Works 452 The Code 453 Notes 455 9-3. Create a Database Connection String Programmatically 455 Problem 455 Solution 455 How It Works 455 The Code 456 9-4. Store a Database Connection String Securely 457 Problem 457 Solution 457 How It Works 457 The Code 458 9-5. Execute a SQL Command or Stored Procedure 460 Problem 460 Solution 461 How It Works 461 The Code 462 9-6. Use Parameters in a SQL Command or Stored Procedure 465 Problem 465 Solution 465 How It Works 465 The Code 466 9-7. Process the Results of a SQL Query Using a Data Reader 469 Problem 469 Solution 469 How It Works 469 The Code 471 9-8. Obtain an XML Document from a SQL Server Query 472 Problem 472 Solution 472 How It Works 473 The Code 474 9-9. Perform Asynchronous Database Operations Against SQL Server 476 Problem 476 Solution 476 How It Works 476 The Code 478 9-10. Write Database-Independent Code 480 Problem 480 Solution 480 How It Works 480 The Code 483 9-11. Discover All Instances of SQL Server on Your Network 484 Problem 484 Solution 484 How It Works 485 The Code 485 9-12. Create an In-Memory Cache 486 Problem 486 Solution 486 How It Works 486 The Code 487 9-13. Create a DataSet Programmatically 490 Problem 490 Solution 490 How It Works 490 The Code 490 9-14. Perform a LINQ Query 492 Problem 492 Solution 493 How It Works 493 The Code 493 9-15. Perform a LINQ Query with Entity Types 495 Problem 495 Solution 495 How It Works 495 The Code 496 9-16. Compare LINQ DataSet Results 497 Problem 497 Solution 497 How It Works 498 The Code 498 Networking 503 10-1. Obtain Information About the Local Network Interface 504 Problem 504 Solution 504 How It Works 504 The Code 506 10-2. Detect Changes in Network Connectivity 508 Problem 508 Solution 508 How It Works 508 The Code 508 10-3. Download Data over HTTP or FTP 510 Problem 510 Solution 510 How It Works 510 The Code 511 Notes 513 10-4. Download a File and Process It Using a Stream 514 Problem 514 Solution 514 How It Works 514 The Code 515 10-5. Respond to HTTP Requests from Within Your Application 516 Problem 516 Solution 517 How It Works 517 The Code 518 10-6. Get an HTML Page from a Site That Requires Authentication 521 Problem 521 Solution 521 How It Works 521 The Code 522 10-7. Send E-mail Using SMTP 523 Problem 523 Solution 524 How It Works 524 The Code 526 10-8. Resolve a Host Name to an IP Address 528 Problem 528 Solution 528 How It Works 529 The Code 529 Usage 530 10-9. Ping an IP Address 531 Problem 531 Solution 531 How It Works 531 The Code 532 Usage 533 10-10. Communicate Using TCP 534 Problem 534 Solution 534 How It Works 534 The Code 535 Usage 538 10-11. Create a Multithreaded TCP Server That Supports Asynchronous Communications 539 Problem 539 Solution 539 How It Works 539 The Code 540 10-12. Communicate Using UDP 547 Problem 547 Solution 547 How It Works 548 The Code 548 10-13. Create a SOAP-Based Web Service 550 Problem 550 Solution 550 How It Works 551 The Code 551 10-14. Call a WCF Service Using a Dynamically Generated Service Proxy 556 Problem 556 Solution 557 How It Works 557 The Code 557 10-15. Process the Content of an Atom or RSS Feed 558 Problem 558 Solution 558 How It Works 558 The Code 560 10-16. Manipulate URIs 562 Problem 562 Solution 562 How It Works 562 The Code 564 Security and Cryptography 567 11-1. Allow Partially Trusted Code to Use Your Strongly Named Assembly 568 Problem 568 Solution 568 How It Works 569 The Code 570 Notes 570 11-2. Disable Code Access Security 571 Problem 571 Solution 571 How It Works 571 11-3. Disable Execution Permission Checks 572 Problem 572 Solution 572 How It Works 572 The Code 572 Notes 573 11-4. Ensure the Runtime Grants Specific Permissions to Your Assembly 573 Problem 573 Solution 574 How It Works 574 The Code 575 11-5. Limit the Permissions Granted to Your Assembly 576 Problem 576 Solution 576 How It Works 576 The Code 577 11-6. View the Permissions Required by an Assembly 578 Problem 578 Solution 578 How It Works 578 The Code 579 Usage 580 11-7. Determine at Runtime If Your Code Has a Specific Permission 582 Problem 582 Solution 582 How It Works 582 The Code 583 11-8. Restrict Who Can Extend Your Classes and Override Class Members 583 Problem 583 Solution 584 How It Works 584 The Code 585 11-9. Inspect an Assembly’s Evidence 586 Problem 586 Solution 586 How It Works 586 The Code 586 11-10. Determine If the Current User Is a Member of a Specific Windows Group 588 Problem 588 Solution 588 How It Works 588 The Code 590 Usage 591 11-11. Restrict Which Users Can Execute Your Code 592 Problem 592 Solution 592 How It Works 592 The Code 594 11-12. Impersonate a Windows User 596 Problem 596 Solution 596 How It Works 596 The Code 597 Usage 599 11-13. Create a Cryptographically Random Number 599 Problem 599 Solution 599 How It Works 600 The Code 600 11-14. Calculate the Hash Code of a Password 601 Problem 601 Solution 601 How It Works 602 The Code 603 Usage 604 11-15. Calculate the Hash Code of a File 605 Problem 605 Solution 605 How It Works 605 The Code 606 Usage 606 11-16. Verify a Hash Code 607 Problem 607 Solution 607 How It Works 607 The Code 608 11-17. Ensure Data Integrity Using a Keyed Hash Code 610 Problem 610 Solution 610 How It Works 610 The Code 611 Usage 612 11-18. Work with Security-Sensitive Strings in Memory 613 Problem 613 Solution 613 How It Works 613 The Code 614 11-19. Encrypt and Decrypt Data Using the Data Protection API 616 Problem 616 Solution 616 How It Works 617 The Code 618 Unmanaged Code Interoperability 621 12-1. Call a Function in an Unmanaged DLL 621 Problem 621 Solution 622 How It Works 622 The Code 622 12-2. Get the Handle for a Control, Window, or File 625 Problem 625 Solution 625 How It Works 625 The Code 625 12-3. Call an Unmanaged Function That Uses a Structure 627 Problem 627 Solution 627 How It Works 627 The Code 627 12-4. Call an Unmanaged Function That Uses a Callback 630 Problem 630 Solution 630 How It Works 630 The Code 631 12-5. Retrieve Unmanaged Error Information 632 Problem 632 Solution 632 How It Works 632 The Code 632 12-6. Use a COM Component in a .NET Client 634 Problem 634 Solution 634 How It Works 634 The Code 635 12-7. Release a COM Component Quickly 637 Problem 637 Solution 637 How It Works 637 12-8. Use Optional Parameters 638 Problem 638 Solution 638 How It Works 638 The Code 638 12-9. Use an ActiveX Control in a .NET Client 639 Problem 639 Solution 639 How It Works 639 12-10. Expose a .NET Component Through COM 640 Problem 640 Solution 640 How It Works 640 Commonly Used Interfaces and Patterns 643 13-1. Implement a Custom Serializable Type 644 Problem 644 Solution 644 How It Works 644 The Code 647 13-2. Implement a Cloneable Type 650 Problem 650 Solution 650 How It Works 650 The Code 652 13-3. Implement a Comparable Type 655 Problem 655 Solution 655 How It Works 655 The Code 656 Usage 658 13-4. Implement an Enumerable Collection 660 Problem 660 Solution 660 How It Works 660 The Code 661 13-5. Implement an Enumerable Type Using a Custom Iterator 664 Problem 664 Solution 664 How It Works 664 The Code 665 13-6. Implement a Disposable Class 671 Problem 671 Solution 671 How It Works 671 The Code 673 13-7. Implement a Formattable Type 675 Problem 675 Solution 675 How It Works 675 The Code 676 Usage 679 13-8. Implement a Custom Exception Class 679 Problem 679 Solution 679 How It Works 679 The Code 681 13-9. Implement a Custom Event Argument 683 Problem 683 Solution 683 How It Works 683 The Code 684 13-10. Implement the Singleton Pattern 685 Problem 685 Solution 685 How It Works 685 The Code 686 Usage 686 13-11. Implement the Observer Pattern 687 Problem 687 Solution 687 How It Works 687 The Code 687 Usage 692 13-12. Implement a Parallel Producer-Consumer Pattern 693 Problem 693 Solution 693 How It Works 693 The Code 694 13-13. Perform Lazy Object Initialization 695 Problem 695 Solution 695 How It Works 696 The Code 696 13-14. Use Optional Parameters 697 Problem 697 Solution 697 How It Works 697 The Code 698 13-15. Add a Method to a Type Without Modifying It 699 Problem 699 Solution 699 How It Works 699 The Code 700 13-16. Call an Object Member Dynamically 701 Problem 701 Solution 701 How It Works 701 The Code 701 13-17. Create a Variant Generic Type 703 Problem 703 Solution 703 How It Works 703 The Code 705 Windows Integration 707 14-1. Access Runtime Environment Information 708 Problem 708 Solution 708 How It Works 708 The Code 710 14-2. Retrieve the Value of an Environment Variable 712 Problem 712 Solution 712 How It Works 712 The Code 713 14-3. Write an Event to the Windows Event Log 714 Problem 714 Solution 714 How It Works 714 The Code 715 14-4. Read and Write to the Windows Registry 716 Problem 716 Solution 716 How It Works 716 The Code 717 14-5. Search the Windows Registry 719 Problem 719 Solution 719 How It Works 719 The Code 721 Usage 723 14-6. Create a Windows Service 723 Problem 723 Solution 723 How It Works 723 The Code 725 Usage 728 14-7. Create a Windows Service Installer 728 Problem 728 Solution 728 How It Works 728 The Code 729 Usage 730 14-8. Create a Shortcut on the Desktop or Start Menu 730 Problem 730 Solution 730 How It Works 731 The Code 731 14-9. Create a Windows 7 Jump List 733 Problem 733 Solution 733 How It Works 733 The Code 734 14-10. Use Windows Search 735 Problem 735 Solution 735 How It Works 736 The Code 737 14-11. Check Internet Connectivity 740 Problem 740 Solution 740 How It Works 740 The Code 740 14-12. Display a Task Dialog 741 Problem 741 Solution 741 How It Works 741 The Code 742 14-13. Write Custom Performance Counters 744 Problem 744 Solution 745 How It Works 745 The Code 745 14-14. Read Performance Counters 748 Problem 748 Solution 748 How It Works 748 The Code 749 14-15. Obtain Elevated Privileges 750 Problem 750 Solution 750 How It Works 750 The Code 751 Parallel Programming 753 15-1. Perform Simple Parallel Tasks 753 Problem 753 Solution 753 How It Works 754 The Code 754 15-2. Return a Result from a Task 756 Problem 756 Solution 756 How It Works 756 The Code 756 15-3. Wait for Tasks to Complete 758 Problem 758 Solution 758 How It Works 758 The Code 758 15-4. Parallel Process a Collection 760 Problem 760 Solution 760 How It Works 760 The Code 761 15-5. Chain Tasks Together 762 Problem 762 Solution 762 How It Works 762 The Code 762 15-6. Write a Cooperative Algorithm 763 Problem 763 Solution 763 How It Works 764 The Code 764 15-7. Handle Exceptions in Tasks 765 Problem 765 Solution 766 How It Works 766 The Code 766 15-8. Cancel a Task 767 Problem 767 Solution 767 How It Works 768 The Code 768 15-9. Share Data Between Tasks 769 Problem 769 Solution 769 How It Works 769 The Code 770 Using LINQ 773 16-1. Perform a Simple LINQ Query 773 Problem 773 Solution 774 How It Works 774 The Code 775 16-2. Filter Items from a Data Source 779 Problem 779 Solution 779 How It Works 779 The Code 779 16-3. Filter a Data Source by Type 781 Problem 781 Solution 781 How It Works 781 The Code 782 16-4. Filter Ranges of Elements 783 Problem 783 Solution 783 How It Works 783 The Code 783 16-5. Select Multiple Member Values 784 Problem 784 Solution 784 How It Works 785 The Code 785 16-6. Filter and Select from Multiple Data Sources 786 Problem 786 Solution 786 How It Works 786 The Code 787 16-7. Use Permutations of Data Sources 789 Problem 789 Solution 789 How It Works 789 The Code 790 16-8. Concatenate Data Sources 791 Problem 791 Solution 791 How It Works 792 The Code 792 16-9. Group Result Elements by Attribute 793 Problem 793 Solution 793 How It Works 793 The Code 794 16-10. Sort Query Results 798 Problem 798 Solution 798 How It Works 798 The Code 798 16-11. Compare Data Sources 800 Problem 800 The Solution 800 How It Works 800 The Code 800 16-12. Aggregate Data Sources 802 Problem 802 Solution 802 How It Works 802 The Code 803 16-13. Share Values Within a Query 804 Problem 804 Solution 805 How It Works 805 The Code 805 16-14. Create Custom LINQ Extension Methods 806 Problem 806 Solution 806 How It Works 806 The Code 807 16-15. Convert from IEnumerable 808 Problem 808 Solution 808 How It Works 808 The Code 808 Windows Presentation Foundation 813 17-1. Create and Use a Dependency Property 814 Problem 814 Solution 814 How It Works 815 The Code 817 17-2. Create and Use an Attached Property 819 Problem 819 Solution 820 How It Works 820 The Code 820 17-3. Define Application-Wide Resources 823 Problem 823 Solution 823 How It Works 823 The Code 824 17-4. Debug Data Bindings Using an IValueConverter 825 Problem 825 Solution 825 How It Works 825 The Code 825 17-5. Debug Bindings Using Attached Properties 827 Problem 827 Solution 827 How It Works 828 The Code 829 17-6. Arrange UI Elements in a Horizontal or Vertical Stack 829 Problem 829 Solution 829 How It Works 829 The Code 830 17-7. Dock UI Elements to the Edges of a Form 831 Problem 831 Solution 831 How It Works 832 The Code 832 17-8. Arrange UI Elements in a Grid 833 Problem 833 Solution 833 How It Works 834 The Code 834 17-9. Position UI Elements Using Exact Coordinates 835 Problem 835 Solution 836 How It Works 836 The Code 836 17-10. Get Rich Text Input from a User 837 Problem 837 Solution 837 How It Works 837 The Code 839 17-11. Display a Contr