BackgroundWorker not working with TeamCity NUnit runner

Posted by Catalin DICU on Stack Overflow See other posts from Stack Overflow or by Catalin DICU
Published on 2010-04-29T09:11:01Z Indexed on 2010/04/29 9:17 UTC
Read the original article Hit count: 411

Filed under:
|
|
|
|

I'm using NUnit to test View Models in a WPF 3.5 application and I'm using the BackgroundWorker class to execute asynchronous commands.The unit test are running fine with the NUnit runner or ReSharper runner but fail on TeamCity 5.1 server.

How is it implemented :

I'm using a ViewModel property named IsBusy and set it to false on BackgroundWorker.RunWorkerCompleted event. In my test I'm using this method to wait for the BackgroundWorker to finish :

protected void WaitForBackgroundOperation(ViewModel viewModel)
{
    int count = 0;
    while (viewModel.IsBusy)
    {
        RunBackgroundWorker();

        if (count++ >= 100)
        {
            throw new Exception("Background operation too long");
        }
        Thread.Sleep(100);
    }
}

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

Well, sometimes it works and sometimes it hangs the build. I suppose it's the Application.DoEvents() but I don't know why...

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#