频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

利用VC# 创作简单的多线程组件

来源:MSDN 作者: 出处:巧巧读书 2006-07-09 进入讨论组
上一页 1 2 3 4 5 6 下一页 
创建 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);
}
观看地址: http://www.qqread.com/csharp/k367144002.html进入讨论组讨论。
上一页 1 2 3 4 5 6 下一页 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章