官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 云南省| 龙江县| 沙田区| 宜宾市| 靖边县| 永福县| 娱乐| 常宁市| 红桥区| 慈利县| 泽普县| 蓝田县| 耿马| 奉新县| 纳雍县| 科技| 灵丘县| 汝南县| 洛川县| 满城县| 聊城市| 梁平县| 延川县| 梁平县| 济阳县| 烟台市| 会昌县| 桓台县| 河津市| 东乡族自治县| 阿拉善右旗| 淮阳县| 新昌县| 延寿县| 同江市| 安泽县| 石嘴山市| 白银市| 申扎县| 新乡市| 凌源市|