簡體   English   中英

BackgroundWorker不與TeamCity NUnit運行器一起使用

[英]BackgroundWorker not working with TeamCity NUnit runner

我正在使用NUnit在WPF 3.5應用程序中測試視圖模型,並且正在使用BackgroundWorker類執行異步命令。單元測試在NUnit運行器或ReSharper運行器上運行正常,但在TeamCity 5.1服務器上失敗。

如何實施:

我正在使用一個名為IsBusyViewModel屬性,並在BackgroundWorker.RunWorkerCompleted事件IsBusy其設置為false。 在我的測試中,我正在使用此方法等待BackgroundWorker完成:

protected void WaitForBackgroundOperation(ViewModel viewModel)
{
    Console.WriteLine("WaitForBackgroundOperation 1");
    int count = 0;
    while (viewModel.IsBusy)
    {
        Console.WriteLine("WaitForBackgroundOperation 2");
        RunBackgroundWorker();
        Console.WriteLine("WaitForBackgroundOperation 3");

        if (count++ >= 100)
        {
            Assert.Fail("Background operation too long");
        }
        Thread.Sleep(1000);

        Console.WriteLine("WaitForBackgroundOperation 4");
    }
}

private static void RunBackgroundWorker()
{
    Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
    System.Windows.Forms.Application.DoEvents();
}

好吧,有時它起作用,有時它掛起了構建。 我想這是Application.DoEvents()但我不知道為什么...

編輯:我添加了一些跟蹤(請參見上面的代碼),並在日志中有:

WaitForBackgroundOperation 1 
WaitForBackgroundOperation 2 
WaitForBackgroundOperation 2 
WaitForBackgroundOperation 2
...

這怎么可能 ?!

您正在啟動一堆后台任務,每秒執行一次。 將RunBackgroundWorker移到循環上方。

protected void WaitForBackgroundOperation(ViewModel viewModel)
{
    Console.WriteLine("WaitForBackgroundOperation 1");
    RunBackgroundWorker();
    Thread.Sleep(100); // wait for thread to set isBusy, may not be needed
    int count = 0;
    while (viewModel.IsBusy)
    {
        Console.WriteLine("WaitForBackgroundOperation 2");
        if (count++ >= 100)
        {
            Assert.Fail("Background operation too long");
        }
        Thread.Sleep(1000);
        Console.WriteLine("WaitForBackgroundOperation 3");
    }
}

private static void RunBackgroundWorker()
{
    Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
}

不需要DoEvents。

暫無
暫無

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

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