lundi 26 août 2019

Don't understand why another thread began to execute the code

I'm working on a web site. In this site you can search for something. This site has a rules system which manipulate the search results. (each rule contains a search results list which being filled after the search is being performed).

The flow goes like this: 1. The user performs a search. 2. The search results are being collected. 3. Collect a search results list for each rule. 4. Apply each rule on its search results list.

The issue is: At step #4 - once in a while, there is another thread which makes a mess in the calculations.

I fixed the issue by manipulating the search results itself instead of manipulating each rule's search results list (explained beneath).

Before the fix:

public void ApplyRules (List<Rule> Rules)
{
   foreach (Rule rule in Rules)
   {
      foreach(SearchResult res in rule.SearchResults)
      {
         ApplyRule(rule, res);
      }
   }
}

The fix:

public void ApplyRules (List<Rule> Rules, List<SearchResult> SearchResults)
{
   foreach (Rule rule in Rules)
   {
      foreach(SearchResult resFromRule in rule.SearchResults)
      {
         SearchResult res = SearchResults.First(r => r.Id.Equals(resFromRule.Id));
         ApplyRule(rule, res);
      }
   }
}

I just want to understand this issue better in order to not repeat this mistake in the future.




Aucun commentaire:

Enregistrer un commentaire