日期:2009-10-30  浏览次数:20445 次

可以编写能同时执行多个任务的应用程序。此能力(称为“多线程处理”或“自由线程处理”)是设计处理器密集型且要求用户输入的组件的强大方法。计算工资表信息的组件就是一个可能利用多线程处理的组件示例。该组件可以在一个线程上处理用户输入到数据库的数据,而在另一个线程上执行频繁使用处理器的工资表计算。通过在不同的线程上运行这些进程,用户不必等到计算机完成计算,就可以输入其他数据。在本演练中,将创建一个简单的多线程组件,该组件可以同时执行若干个复杂计算。


创建项目
应用程序将包括单个窗体和一个组件。用户将输入值并指示该组件开始计算。然后,窗体将接收来自该组件的值,将其显示在标签控件中。该组件将执行频繁使用处理器的计算,并在完成后通知窗体。您将在组件中创建公共变量,用以保存从用户界面收到的值。同时,您还将在组件中实现一些方法,根据这些变量的值执行计算。

注意 尽管对于计算值的方法来说,函数通常更为可取,但不能在线程之间传递参数,也不能返回值。有很多向线程提供值和从线程接收值的简单方法。在本演示中,将通过更新公共变量将值返回到用户界面,当线程执行完毕后,使用事件来通知主程序。
创建窗体

创建新的“Windows 应用程序”项目。
将应用程序命名为 Calculations,并将 Form1.cs 重命名为 frmCalculations.cs。
该窗体将用作应用程序的主用户界面。

双击设计器上的窗体以打开代码编辑器。在“编辑”菜单中,选择“查找和替换”,然后选择“替换”。使用“全部替换”将 Form1 替换为 frmCalculations。
在“解决方案资源管理器”中,右击“frmCalculations.cs”并选择“视图设计器”。设计器打开。
向窗体中添加 5 个 Label 控件、4 个 Button 控件和 1 个 TextBox 控件。
为这些控件设置属性,如下所示:
控件 名称 文本
Label1 lblFactorial1 (空白)
Label2 lblFactorial2 (空白)
Label3 lblAddTwo (空白)
Label4 lblRunLoops (空白)
Label5 lblTotalCalculations (空白)
Button1 btnFactorial1 Factorial
Button2 btnFactorial2 Factorial - 1
Button3 btnAddTwo Add Two
Button4 btnRunLoops Run a Loop
Textbox1 txtValue (空白)

创建 Calculator 组件

从“项目”菜单中选择“添加组件”。
将组件命名为 Calculator。
向 Calculator 组件添加公共变量

为 Calculator 打开代码编辑器。
添加创建公共变量的语句,这些变量用于将值从 frmCalculations 传递给每个线程。
变量 varTotalCalculations 将保留该组件执行的计算总数的累计值,而其他变量将接收来自窗体的值。

public int varAddTwo;
public int varFact1;
public int varFact2;
public int varLoopValue;
public double varTotalCalculations = 0;
向 Calculator 组件添加方法和事件

为事件声明委托,组件将使用这些事件向窗体传递值。
注意 尽管您将声明 4 个事件,但由于其中的两个事件将具有相同的签名,因此只需要创建 3 个委托。
紧接着上一步输入的变量声明的下方,键入下列代码:

// This delegate will be invoked with two of your events.
public delegate void FactorialCompleteHandler(double Factorial, double TotalCalculations);
public delegate void AddTwoCompleteHandler(int Result, double TotalCalculations);
public delegate void LoopCompleteHandler(double TotalCalculations, int Counter);
声明组件将用来与应用程序进行通信的事件。为实现此目的,紧接着上一步输入的代码的下方,添加下列代码。
public event FactorialCompleteHandler FactorialComplete;
public event FactorialCompleteHandler FactorialMinusOneComplete;
public event AddTwoCompleteHandler AddTwoComplete;
public event LoopCompleteHandler LoopComplete;
紧接着上一步键入的代码的下方,键入下列代码:
// This method will calculate the value of a number minus 1 factorial
// (varFact2-1!).
public void FactorialMinusOne()
{
double varTotalAsOfNow = 0;
double varResult = 1;
// Performs a factorial calculation on varFact2 - 1.
for (int varX = 1; varX <= varFact2 - 1; varX++)
{
varResult *= varX;
// Increments varTotalCalculations and keeps track of the current
// total as of this instant.
varTotalCalculations += 1;
varTotalAsOfNow = varTotalCalculations;
}
// Signals that the method has completed, and communicates the
// result and a value of total calculations performed up to this
// point.
FactorialMinusOneComplete(varResult, varTotalAsOfNow);
}

// This method will calculate the value of a number factorial.
// (varFact1!)
public void Factorial()
{
double varResult = 1;
double varTotalAsOfNow = 0;
for (int varX = 1; varX <= varFact1; varX++)
{
varResult *= varX;
varTotalCalculations += 1;
varTotalAsOfNow = varTotalCalculations;
}
FactorialComplete(varResult, varTotalAsOfNow);
}

// This method will add two to a number (varAddTwo+2).
public void AddTwo()
{