- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 478字
- 2021-07-02 15:29:27
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:

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:

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:

Again to review, we did the following in this program:
- First, we called the vals.ForEach(ShowSquare) bit, which generates a list.
- Then we called the block beginning with vals.ForEach(delegate (double x), as an anonymous function or method that generates a list.
- 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.
- 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.
- Learning Cython Programming
- R語言數(shù)據(jù)可視化之美:專業(yè)圖表繪制指南
- 羅克韋爾ControlLogix系統(tǒng)應用技術
- PHP+MySQL網(wǎng)站開發(fā)技術項目式教程(第2版)
- Java編程指南:基礎知識、類庫應用及案例設計
- Java Web應用開發(fā)技術與案例教程(第2版)
- Mastering Apache Spark 2.x(Second Edition)
- Asynchronous Android Programming(Second Edition)
- Hadoop 2.X HDFS源碼剖析
- 區(qū)塊鏈架構之美:從比特幣、以太坊、超級賬本看區(qū)塊鏈架構設計
- Unity Android Game Development by Example Beginner's Guide
- 人人都能開發(fā)RPA機器人:UiPath從入門到實戰(zhàn)
- 軟硬件綜合系統(tǒng)軟件需求建模及可靠性綜合試驗、分析、評價技術
- Building a Media Center with Raspberry Pi
- 軟件再工程:優(yōu)化現(xiàn)有軟件系統(tǒng)的方法與最佳實踐