创建 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() { double varTotalAsOfNow = 0; int varResult = varAddTwo + 2; varTotalCalculations += 1; varTotalAsOfNow = varTotalCalculations; AddTwoComplete(varResult, varTotalAsOfNow); } // This method will run a loop with a nested loop varLoopValue times. public void RunALoop() { int varX; double varTotalAsOfNow = 0; for (varX = 1; varX <= varLoopValue; varX++) { // This nested loop is added solely for the purpose of slowing down // the program and creating a processor-intensive application. for (int varY = 1; varY <= 500; varY++) { varTotalCalculations += 1; varTotalAsOfNow = varTotalCalculations; } } LoopComplete(varTotalAsOfNow, varLoopValue); } |
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- C#变得越来越臃肿是不可避免的? (7次浏览)
- C#与EXCEL的数据交互(一) (5次浏览)
- C# 3.0新特性之扩展方法 (1次浏览)
- C#调用QTP自动化对象模型的实例 (0次浏览)
- 利用 C# 实现任务栏通知窗口 (0次浏览)
- 深入C#学习系列之不可小瞧的using关键字 (0次浏览)
- 如何用.NET技术在线生成网站LOGO (0次浏览)
- C#实现遗传算法 模拟花朵的进化 (0次浏览)
- c#里的运算符重载 (0次浏览)
- QTP调用自己开发的.net类库 (0次浏览)



