Beginning C# 7 Hands-On:Advanced Language Features
上QQ阅读APP看书,第一时间看更新

Running and modifying the program

Now, let's run the program. For this, crank it up in your browser and click on the Show Results button. Take a look at the results, as shown in Figure 8.3.2. There's one slight issue with the program as it's written. We will learn the reason for this problem momentarily and then fix it:

Figure 8.3.2: The initial run of our program

Now, there's one more function I want to tell you about, Join. Enter the following as the next line:

td.Join();

Now, if you hover your mouse over Join, the pop-up tip says Blocks the calling thread until the thread terminates, while continuing to perform standard COM and Send, Message Pumping. If you hover your mouse over Start, the pop-up tip says Causes the operating system to change the state of the current instance to ThreadState.Running. In other words, in the Thread td = new Thread(delegate () block, Thread is an object. In this case, you're making a new thread that has a delegate, so it runs off in its own thread of processing, away from the main program. So, it's kind of interesting.

Now, notice that, when we printed that stuff, there were only really two main lists, with the second one essentially appended to the first. So, let's do it this way; otherwise, we will not be able to see the effect clearly. Under the preceding vals.ForEach(ShowSquare) line, enter the following:

sampLabel.Text += "<br>------------------------------------------------------";

Note that I separated this with a long-dashed line in quotes.

Next, after this one, let's do one more beneath the closing curly brace, parenthesis, and semicolon after the sampLabel.Text += "<br>" + Math.Pow(x, 3) line.

sampLabel.Text += "<br>-------------------------------------------------------";

Now, if we remove td.Join() and run the program, there are only two lists, as shown in Figure 8.3.3. There should be three of them, however:

Figure 8.3.3: The modified run shows only two lists

So re-insert td.Join(); and take a look at it again in your browser. Now, as you can see in Figure 8.3.4, there are three lists, as there should be:

Figure 8.3.4: The final program run shows three separate lists

Again to review, we did the following in this program:

  1. First, we called the vals.ForEach(ShowSquare) bit, which generates a list.
  2. Then we called the block beginning with vals.ForEach(delegate (double x), as an anonymous function or method that generates a list.
  3. Next, with the block beginning with Thread td = new Thread(delegate (), we created this anonymous object called td, which is a Thread class that has its own anonymous method inside, which runs in its own separate thread.
  4. Finally, we started it, and the Join function blocks the current thread, waiting for the Thread td = new Thread(delegate () block to execute, and then it resumed, so that everything was displayed.

These are the fundamentals of anonymous constructs of this kind.