Edit: The solution I went for including tweaks.
If you follow the comments, my issue is that there is once process that takes all the time in my ProcessMessage method. There is barely anything I can do while I'm waiting for it to do it's stuff so async was not much of an improvement.
I went for a Reactive Extension solution instead.
Note - I need to return a method that returns Enumberable
.
private void GetFlighData()
{
while (!_cancel)
{
Parallel.ForEach(F.MessageFactory.GetDueMessages(), dueMessage =>
{
var item = ProcessMessage(dueMessage);
if (item != null)
{
OnGetFlightData(item);
}
});
}
}
private delegate void GetAFlightDataHandler(FlightData flightData);
private event GetAFlightDataHandler FlightData;
private void OnGetFlightData(FlightData flightData)
{
if (FlightData != null)
FlightData(flightData);
}
BackgroundWorker _worker = new BackgroundWorker();
public override GetFlighDataMethod GetFlighDataMethod
{
get
{
_worker.DoWork += delegate { GetFlighData(); };
_worker.RunWorkerAsync();
return () =>
{
var flightDataReceived = Observable.FromEvent<GetAFlightDataHandler, FlightData>(handler =>
{
GetAFlightDataHandler fdHandler = (e) =>
{
handler(e);
};
return fdHandler;
},
fdHandler => FlightData += fdHandler,
fdHandler => FlightData -= fdHandler
);
return flightDataReceived.ToEnumerable();
};
}
}