msdn中在演练BackgroundWorker的时候,使用递归计算Fibonacci数列的f(n)。
private void startAsyncButton_Click(object sender, EventArgs e)
{
//清空
resultLabel.Text = String.Empty;
//设置状态
private int numberToCompute = 0;
private int highestPercentageReached = 0;
this.numericUpDown1.Enabled = true;//numericUpDown1为读入需要计算的值
this.startAsyncButton.Enabled = false;//开始计算按钮
this.cancelAsyncButton.Enabled = true;//取消按钮
numberToCompute =(int) numericUpDown1.Value;
// Reset the variable for percentage tracking.
highestPercentageReached = 0;
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute);
}
private void cancelAsyncButton_Click(object sender, EventArgs e)
{
// Cancel the asynchronous operation.
this.backgroundWorker1.CancelAsync();
// Disable the Cancel button.
this.cancelAsyncButton.Enabled = false;
this.startAsyncButton.Enabled = true; ;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = ComputeFibonacci((int)e.Argument, worker, e);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
resultLabel.Text = "Canceled";
}
else
{
// Finally, handle the case where the operation
// succeeded.
resultLabel.Text = e.Result.ToString();
}
this.numericUpDown1.Enabled = true;
startAsyncButton.Enabled = true;
cancelAsyncButton.Enabled = false;
}
// This is the method that does the actual work. For this
// example, it computes a Fibonacci number and
// reports progress as it does its work.
long ComputeFibonacci(int n, BackgroundWorker worker, DoWorkEventArgs e)
{
// The parameter n must be >= 0 and <= 91.
// Fib(n), with n > 91, overflows a long.
if ((n < 0) || (n > 91))
{
throw new ArgumentException(
"value must be >= 0 and <= 91", "n");
}
long result = 0;
if (worker.CancellationPending)
{
e.Cancel = true;
}
else
{
if (n < 2)
{
result = 1;
}
else
{
result = ComputeFibonacci(n - 1, worker, e) +
ComputeFibonacci(n - 2, worker, e);
}
// Report progress as a percentage of the total task.
int percentComplete =
(int)((float)n / (float)numberToCompute * 100);
if (percentComplete > highestPercentageReached)
{
highestPercentageReached = percentComplete;
worker.ReportProgress(percentComplete);
}
}
return result;
}
//
//当然这是为了演示BackgroundWorker,其实Fibonacci数列可以如下这样计算:
private void FibonacciForm_Load(object sender, EventArgs e)
{
//textBox1.Text = "斐波纳契数列(一种整数数列, 其中每数等于前面两数之和)。"
// + "该数列为 1、1、2、3、5、8、........"
// + "如果你想知道该数列的第n个数,请在下面输入n-1";
}
private void button1_Click(object sender, EventArgs e)
{
numberToCompute = (int)numericUpDown1.Value;
double tem = Fibonacci_2(numberToCompute);
label1.Text = tem.ToString();
}
private double Fibonacci_2(int n)
{
if (n < 2)
return 1;
else
return (Math.Pow((Math.Sqrt(5) + 1)/ 2.0, n) - Math.Pow((1 - Math.Sqrt(5))/ 2.0, n) ) / Math.Sqrt(5);
}