Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
i have one foreach loop which is taking currently more time and i am thinking to change to Parallel.ForEach, so is there any way to share the values of variables like <b>finalResult</b> and <b>testResult</b> among the parallel task, i.e each task get the value from shared location and update it,so that next task can pick the updated value an so on.
Could someone please help me how to achieve this?
C#
protected Data mytest(ref Check testResult, CancellationToken token, bool logOnError, bool logOnSuccess)
{
           Data finalResult = new Data(ActionStatus.Success);
            foreach (Check data in mydata.Values)
            {

                Data result = watcher.ExecuteOnce();
                Logger.Write(LogType.Info,result.Status == Success ? "succeeded" : "failed");
                finalResult = GetHighestPriority(finalResult, result, ref testResult, data);


            }
            return finalResult;
}


Thanks..
Posted
Updated 26-Nov-14 21:20pm
v2

1 solution

Hi
Use volatile keyword

C#
public volatile int i;

       public void Test(int _i)
       {
           i = _i;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900