簡體   English   中英

如何取消等待后台工作者的線程?

[英]How to cancel a thread waiting for a background worker?

我在Visual Studio 2017中使用WinForms在C#中編寫了一個程序。
任務是在程序啟動時(加載表格時)開始計算斐波那契數。

當我通過文本框輸入數字時,該文本框已經計算並寫入數組。 顯示數字。
當我輸入一個尚未計算的數字時,我將啟動一個等待結果的等待線程,然后更新結果標簽。
當計算正在進行時,我將按鈕更改為“取消”(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;
            }
        }


    }
}

使用其他背景工作者

看起來,您與背景工作者一起工作更自在。 您可以在此處使用其他后台工作者而不是線程。

因此,與其創建線程:

Thread t1 = new Thread(waitingMethod);

您可以創建后台工作人員並在此處啟動它:

waitingWorker.RunWorkerAsync();

然后,您可以處理worker的事件,並使用其CancellationPending標志取消線程:

private void waitingWorker_DoWork(object sender, DoWorkEventArgs e)
{
    while (arrFibo[eingabe] == 0 && !waitingWorker.CancellationPending)
    {
        Thread.Sleep(500);
        Console.WriteLine("Treadsleeping " + arrFibo[eingabe]);
    }
}
private void waitingWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    labelErgebnis.Invoke(new Action(() => labelErgebnis.Text = arrFibo[eingabe].ToString()));
    ...
}

使用線程

如果仍要使用線程,則需要自己實現一個取消標志。 您可以應用來自較早一個問題的解決方案之一: 如何取消線程?

到目前為止,您的代碼中從未使用過_shouldStop 您可以將其用作取消標志。 使用@FredrikMörk的答案中的代碼

bool _shouldStop = false;
private static object _shouldStopLock = new object();

//in buttonStartStop_Click2
lock (_shouldStopLock)
{
    _shouldStop = false;
}

//in waiting method
bool localShouldStop = false;
while (arrFibo[eingabe] == 0 && !localShouldStop)
{
    Thread.Sleep(500);
    Console.WriteLine("Treadsleeping " + arrFibo[eingabe]);
    lock (_shouldStopLock)
    {
        localShouldStop = _shouldStop;
    }
}    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM