BackgroundWorker阻塞问题 - 集成测试和Real App之间的不一致

时间:2014-05-13 14:23:17

标签: c# backgroundworker

我遇到了一个奇怪的情况。我尝试使用BackgroundWorker实例列表,在它们上调用RunWorkerAsync(),然后在将响应返回给调用者之前等待所有这些实例完成。当我从NUnit集成测试中调用该方法时,这种方法很有效,该测试可以运行worker中的所有代码。但是,当我从我的Web应用程序执行时,相同的代码会不断阻塞主线程,并且RunWorkerCompleted回调永远不会触发。为什么是这样?这是我的代码(这里仅使用1个BackgroundWorker来说明,但我实际上只有> 1时才使用它):

为BackgroundWorkers提供新闻并以阻止方式执行它们的主要代码:

// Build a list of BackgroundWorker instances representing the work needing to be done
var workers = new List<BackgroundWorker>
{
    GetContactsByFullNameAsync(lastName, firstName),
};

// Execute the work to be done (blocking during execution)
this.ExecuteAsyncWork(workers);

// Aggregate up the contacts and return the final result
return this.AggregateContacts(result);

用于新建BackgroundWorker的私有功能(但不执行它):

private BackgroundWorker GetContactsByFullNameAsync(string lastName, string firstName)
{
    var worker = new BackgroundWorker();
    worker.DoWork += (sender, args) =>
    {
        var result = new SuperSearchResultDTO();
        IList<Contact> contacts = _contactRepository.GetContactsByFullName(lastName, firstName);
        // Transform any resulting Contact instances to ContactDTO instances
        if (contacts != null && contacts.Count != 0)
            contacts.ToList().ForEach(c => result.Contacts.Add(ContactDTO.GetFromContact(c)));
        args.Result = result;
    };
    worker.RunWorkerCompleted += (sender, e) =>
    {
        if (e.Error != null)
            // An error was thrown inside the repository, but since it was thrown in a separate thread, it 
            // won't stop the current thread from executing as per usual.  We need to log the exception with 
            // the results, and handle it later.  Throwing right here doesn't do anything.
            _asyncWork[source] = new SuperSearchResultDTO { Exception = e.Error };
        else if (e.Cancelled)
            _asyncWork[source] = new SuperSearchResultDTO { Exception = new Exception("GetSSOContactsByEmailAsync was cancelled") };
        else
            // Cast the results from type "object" to SuperSearchResultDTO
            _asyncWork[source] = (SuperSearchResultDTO)e.Result;
    };
    return worker;
}

执行所有工作程序的私有方法,阻塞直到完成并处理异常:

private void ExecuteAsyncWork(List<BackgroundWorker> workers)
{
    // Kick of the searches against the different data sources simultaneously
    workers.ForEach(x => x.RunWorkerAsync());

    // BLOCK the current thread until either all the async methods are complete 
    while (workers.Any(x => x.IsBusy))
        System.Threading.Thread.Sleep(50);

    // Handle any exceptions
    var exception = _asyncWork.Where(x => x.Value != null && x.Value.Exception != null).Select(x => x.Value.Exception).FirstOrDefault();
    if (exception != null)
        throw exception;
}

1 个答案:

答案 0 :(得分:1)

你只是在主线程上调用Thread.Sleep,所以它会阻塞。您需要使用回调或事件来解决此问题。