BackgroundWorker第一次工作,但第二次失败

时间:2013-12-17 09:21:30

标签: c# wpf multithreading backgroundworker

我在实现BackgroundWorker类时遇到了一个奇怪的问题。在我的程序中会运行一些测试(对于某些对这个问题不重要的硬件)。 这是我的BackgroundWorker代码:

public AllTests(MainMenu p)
    {
        InitializeComponent();
        WindowStartupLocation = WindowStartupLocation.CenterScreen;
        this.parent = p;

        generateCaseLabels(tc.getTCs());

        var testThread = new BackgroundWorker();

        // Tell the testthread what to do
        testThread.DoWork += (sender, args) =>
        {
            List<BaseTestCase> cases = tc.getTCs();

            foreach ( BaseTestCase test in cases )
            {
                test.execute();
                while (!test.Done) { Thread.Sleep(500); }

                if (test.Passed)
                {
                    passed.Add(test);
                }
                else
                {
                    failed.Add(test);
                }
            }
        };

        // Start the TestReport after the testing is completed
        testThread.RunWorkerCompleted += (sender, args) =>
        {
            TestReport newWindow = new TestReport(parent, passed, failed);
            testThread.CancelAsync(); // reset the testThread (?)
            log.append(this, "--- DONE TESTING --------------------");
            newWindow.Show();
            this.Close();
        };

        // Start the testthread
        testThread.WorkerSupportsCancellation = true;
        testThread.RunWorkerAsync();

    }

这段代码发生的奇怪事情是第一次启动它时效果很好,但是当再次导航到这个AllTests窗口时,TestReport窗口立即打开,表明testThread.RunworkerCompleted窗口1}}立即被调用。所以我的问题是:有没有人知道重置BackgroundWorker或其他东西是否聪明,或者对这种奇怪的行为有答案?

顺便说一句,这就是我如何调用AllTests窗口:

 private void startTests(object sender, RoutedEventArgs e)
    {
        log.append(this, "--- STARTING ALL TESTS --------------------");
        AllTests newWindow = new AllTests(parent);
        newWindow.Show(); this.Close(); 
    }

感谢您的帮助! Robbert。

PS如果我犯了任何拼写错误或问题不够清楚;请原谅 - 英语不是我的母语,这是我在StackOverflow上的第一篇文章!谢谢!

1 个答案:

答案 0 :(得分:0)

这取决于您的“AllTests”窗口是如何关闭的。例如,在这个简化的测试中,它每次都可以工作并调用后台工作者:

// simple main form with a button to open the 2nd form
public partial class MainWindow : Window
{
    public MainWindow() { InitializeComponent(); }
    private void Button_Click(object sender, RoutedEventArgs e) { new PopupWindow().Show(); }
}

// 2nd popup window constructor (hence background worker) fires after form is closed and re-opened from the main form
public partial class PopupWindow : Window
{
    public PopupWindow()
    {
        InitializeComponent();
        WindowStartupLocation = WindowStartupLocation.CenterScreen;

        var testThread = new BackgroundWorker();
        {
            testThread.DoWork += (sender, args) => { MessageBox.Show("doWork running"); };
            testThread.RunWorkerCompleted += (sender, args) => { MessageBox.Show("doWork completed"); };
            testThread.WorkerSupportsCancellation = true;
            testThread.RunWorkerAsync();
        }
    }
}