I have an interface, IDataQuery which has an Event to nofity listeners when data loading is progressing and complete. The loading is done via a BackgroundWorker, and fired appropriately, in subclasses of IDataQuery.

public interface IDataQuery : IDisposable
{
  event EventHandler<DataLoadCompletedEventArgs> LoadCompleted;
  event EventHandler<DataLoadProgressEventArgs> LoadProgressChanged;
}

DataLoadCompletedEventArgs and DataLoadProgressEventArgs derive from EventArgs, and have data/progress info associated with them.

I'm currently not capturing errors that could occur on the background thread, and the LoadCompleted EventHandler is fired, and I'm not checking the RunWorkerCompletedEventArgs to see that it has an Error. I want to gracefully notify the consumer that an error ocurred.

What is the best design to handle errors in these background tasks? Should I:

  1. Add my own Error/Exception property to LoadCompleted, and required the consumer to check it
  2. have a new EventHandler / EventArgs specifically for the failure case
  3. have DataLoadCompletedEventArgs derive from AsyncCompletedEventArgs (which has an Error property), and keep the "Completed with error" and "Completed with success" cases bundled?
  4. Something else?
有帮助吗?

解决方案

Option 3 seems to fit best with the Microsoft recommended best practices for this pattern:

See the "Errors and Exceptions" section of this MSDN page:

Best Practices for Implementing the Event-based Asynchronous Pattern

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top