Same Result is getting override When executing multiple requests using Parallel.Foreach -


problem: same result getting override when executing multiple requests using parallel.foreach

i continuously accepting requests , launching new task each request using task factory , using parallel.foreach() execute these requests on multiple processor (as per code's logic)

--first launching each request using task.factory task.factory.startnew(() => processprocessorsinparallel(e.configkey, content));

--second execution begin here ,each request getting executed using parallel.foreach , here p.result getting override multile requests ,getting same result(p.result) incorrect.

    private void processprocessorsinparallel(string configkey, string content)     {          var processes = getflprocessor().processors.where(p => p.enabled).tolist();         parallel.foreach(processes, (p) =>         {             p.process(content, configkey);             var p1 = new pb();           var result = checkresponse.parsefrom(base64.decodebase64(p.result));          }     } 

should use lock inside parallel.foreach? if yes how can achieve parallelism?

here result(p.result) property getting set process method , trying access result proeperty after process method call. mulitples request, result getting same multiples request.

processor class , process method details mentioned below:

public  class processor   {   public string result     {         get;         set;     }    public void process(string inputtrade, string key)     {       result = rulechecker.check(inputtrade, new list<type>() { 'abc" }, previoustradexml).tobytestring().tobase64();      }   } 

if there scope of parallelizing processing, parallelism achieved when attempting parallel.foreach(). however, answer correct (i.e. match answer of serial execution) if allow 1 thread enter critical section of code @ time.

to attain mutual exclusion, have use locks on shared resources. these shared resources may not limited data structure, monitor if, example, printing on screen.

update question further if want more specific answer.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -