【发布时间】:2018-05-13 18:13:18
【问题描述】:
我在 Visual Studio 2017 中使用 WinForms 用 C# 编写程序。
任务是斐波那契数在程序启动时开始计算(当表单加载时)
当我通过文本框输入数字时,该数字已经计算并写入数组。显示数字。
当我输入一个尚未计算的数字时,我会启动一个正在等待结果的等待线程,然后更新结果标签。
当计算正在进行时,我将按钮更改为 Cancel(Abbrechen) 按钮。但我不能真正取消等待线程。
知道如何取消线程 t1?
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace Einsendeaufgabe_GPI13_4
{
public partial class Form1 : Form
{
int eingabe;
long[] arrFibo;
bool calculationComplete = false;
bool _shouldStop = false;
public Form1()
{
InitializeComponent();
this.backgroundWorker1.RunWorkerAsync(); // Start BackGroundworker
}
// Start Button
private void buttonStartStop_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy || calculationComplete)
{
try
{
eingabe = int.Parse(textBoxEingabe.Text);
buttonStartStop.Text = "Abbrechen";
buttonStartStop.Refresh();
labelErgebnis.Text = "";
buttonStartStop.Click -= buttonStartStop_Click;
buttonStartStop.Click += buttonStartStop_Click2;
if (arrFibo[eingabe] == 0)
{
labelErgebnis.Text = "Calculating...";
labelErgebnis.Refresh();
Thread t1 = new Thread(waitingMethod); // new thread, so if result is not calculated yet, teh waiting will be done in different thread
t1.Start();
}
else
{
labelErgebnis.Text = arrFibo[eingabe].ToString();
buttonStartStop.Text = "Berechnen";
labelErgebnis.Refresh();
buttonStartStop.Click -= buttonStartStop_Click2;
buttonStartStop.Click += buttonStartStop_Click;
}
}
catch (FormatException)
{
MessageBox.Show("Ungültige Eingabe. Nur Zahlen eingeben", "Eingabefehler");
}
}
}
//change event back to Click and cancel thread
private void buttonStartStop_Click2(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
buttonStartStop.Text = "Berechnen";
labelErgebnis.Text = "";
buttonStartStop.Click -= buttonStartStop_Click2;
buttonStartStop.Click += buttonStartStop_Click;
_shouldStop = true;
}
}
//waiting mehtod, when waiting for input is calculated
public void waitingMethod()
{
while (arrFibo[eingabe] == 0)
{
Thread.Sleep(500);
Console.WriteLine("Treadsleeping " + arrFibo[eingabe]);
}
labelErgebnis.Invoke(new Action(() => labelErgebnis.Text = arrFibo[eingabe].ToString()));
labelErgebnis.Invoke(new Action(() => buttonStartStop.Text = "Berechnen"));
labelErgebnis.Invoke(new Action(() => labelErgebnis.Refresh()));
buttonStartStop.Click -= buttonStartStop_Click2;
buttonStartStop.Click += buttonStartStop_Click;
}
// Fibonacci mehtod
public long calcFibo(BackgroundWorker bw, int n)
{
while (!bw.CancellationPending)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
long a = (calcFibo(bw, (n - 1)) + calcFibo(bw, (n - 2)));
arrFibo[n] = a;
}
break;
}
return arrFibo[n];
}
// Backgroundworker started at programstart
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
arrFibo = new long[92];
arrFibo[0] = 0;
arrFibo[1] = 1;
calcFibo(bw, 91);
if (bw.CancellationPending)
{
e.Cancel = true;
}
}
// When Backgroundworker Thread is finished
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
Console.WriteLine("Cancelled");
}
else
{
calculationComplete = true;
}
}
}
}
【问题讨论】:
-
在您的代码中,我看到您正在设置
_shouldStop = true但您没有在以后的代码中使用它来真正停止等待线程!如果我处于类似情况,我会创建等待线程Thread t1作为类级别线程,并且您设置_shouldStop = true,我会写t1.Abort()。 -
@Amit - 请不要写
t1.Abort()。只有在您尝试强制退出整个应用程序时,才应该调用.Abort()。否则会使 .NET 运行时处于无效状态。 -
@Enigmativity 这对我来说是新信息..!谢谢!
标签: c# multithreading winforms backgroundworker fibonacci