I have some code where I search for some event frames, then try to process the results.
The issue I have is that processing the IEnumerable results is very, very slow. It consistently takes my application time from 5 seconds to 45 seconds.
Here is the code:
public void LoadClosedTasks(CurrentShift currentShift)
{
//CodeTimer LoadClosedTasksTimer = new CodeTimer();
if (currentShift == null) return;
List<AFSearchTokenBase> tokens = AppliedSearchTokens.ToList(); //Copy the list
tokens.RemoveAll(x => x.Filter == AFSearchFilter.InProgress); //Remove tokens for "In Progress"
tokens.Add(new AFSearchValueToken("|COMPLETED_SHIFT_ID", currentShift.ShiftID.ToString()));
IEnumerable<AFEventFrame> RetrievedFrames = null;
using (AFEventFrameSearch search = new AFEventFrameSearch(LogDatabase, "baseSearch", tokens))
{
//DateTime searchStart = DateTime.Now;
RetrievedFrames = search.FindObjects(0, true, 0);
}
ConcurrentBag<OperatorTask> createdFrames = new ConcurrentBag<OperatorTask>(); //Add to concurrent bag to avoid multi-thread issues.
return;
Parallel.ForEach(RetrievedFrames, frame =>
{
//OperatorTask frameInstance = new OperatorTask();
//frameInstance.PopulateFromFrame(MainForm, frame, this);
//createdFrames.Add(frameInstance);
});
}For info, this routine is called using an await Task.Run.
You can see where I place the "return" statement in the code, to bypass the Parallel.ForEach() block. With return there, the application loads (doing lots of other stuff) in 4.5 seconds. If I remove the return statement, the application takes 45 seconds. And this is with the code body in the Parallel.ForEach block commented out, as it shows above! It is not the code in the block that is slow, because there is no code running and I still get the problem.
I also tried a synchronous ForEach across the collection. Same issue.
This suggests to me that the simple act of enumerating the returned search is taking 40 seconds. (Timing it confirms that).
So... how many elements is it iterating?
Glad you asked. The search results are returning exactly 1 result!
Any idea on what is happening here? Search.FindObjects does have LoadFull set to true, but switching it to false doesn't help. Setting the page size doesn't help. I really am at a loss on how to fix this.