CHAPTER 14 BUILDING MULTITHREADED APPLICATIONS 465 (Multiple domain web hosting) Figure

CHAPTER 14 BUILDING MULTITHREADED APPLICATIONS 465 Figure 14-7. Multithreaded applications provide results in more responsive applications. Before we move on, it is important to note that when you build multithreaded applications (which includes the use of asynchronous delegates) on single CPU machines, you do not end up with an application that runs any faster, as that is a function of a machine s CPU. When running this application using either one or two threads, the numbers are still displaying at the same pace. In reality, multithreaded applications result in more responsive applications. To the end user, it may appear that the program is faster, but this is not the case. Threads have no power to make foreach loops execute quicker, to make paper print faster, or to force numbers to be added together at rocket speed. Multithreaded applications simply allow multiple threads to share the workload. Source Code The SimpleMultiThreadApp project is included under the Chapter 14 subdirectory. Working with the ParameterizedThreadStart Delegate Recall that the ThreadStart delegate can point only to methods that return void and take no arguments. While this may fit the bill in many cases, if you wish to pass data to the method executing on the secondary thread, you will need to make use of the ParameterizedThreadStart delegate type. To illustrate, let s re-create the logic of the AsyncCallbackDelegate project created earlier in this chapter, this time making use of the ParameterizedThreadStart delegate type. To begin, create a new console application named AddWithThreads and use the System.Threading namespace. Now, given that ParameterizedThreadStart can point to any method taking a System. Object parameter, you will create a custom type containing the numbers to be added: class AddParams { public int a; public int b; public AddParams(int numb1, int numb2) { a = numb1; b = numb2; } } Next, create a static method in the Program class that will take an AddParams type and print out the summation of each value:
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Leave a Reply