none
Some times the action body of Parallel.ForEach is not executing even IEnumerable method yield the required parameter. RRS feed

  • Question

  • private void ProcessExecutor()
            {
                int parallel = _Query.MaxDegreeOfParallelism;
                ParallelOptions options = new ParallelOptions();
                switch (parallel)
                {
                    case -1:
                        options.MaxDegreeOfParallelism = Environment.ProcessorCount;
                        Parallel.ForEach(GetProcessModule(), options, ExecuteProcess);
                        break;
                    case 0:
                        Parallel.ForEach(GetProcessModule(), ExecuteProcess);
                        break;
                    default:
                        parallel = Math.Abs(parallel);
                        options.MaxDegreeOfParallelism = parallel;
                        Parallel.ForEach(GetProcessModule(), options, ExecuteProcess);
                        break;
                }
            }

            private IEnumerable<ProcessModule> GetProcessModule()
            {
                ProcessModule process = null;
                while (true)
                {
                    lock (_LockObject)
                    {
                        process = _DDMatrix.GetReadyProcess();
                        if (process != null)
                        {
                             _RunningProcessCount++;
                        }
                    }

                    if (process == null)
                    {
                        if (_RunningProcessCount == 0)
                        {
                            Logger.Write("no process is ready AND no process is running.hence, processing over",
                                         QdalDsmConstants.LOGGER_CATEGORY_BLL);
                            break;
                        }
                        else
                        {
                            Thread.Sleep(5000);
                            continue;
                        }
                    }

                    yield return process;

                    // Before going to next iteration, just wait for some time..
                    Thread.Sleep(5000);
                }
            }

    private void ExecuteProcess(ProcessModule process, ParallelLoopState state)
            {
                if (state != null && _StopProcessing)
                {
                    state.Stop();
                    return;
                }

                // Some logic code is there

                lock (_LockObject)
                {
                    _RunningProcessCount--;
                }
            }

    In the above code sample, some times even GetProcessModule yield the process object, the ExecuteProcess is not invoking. Please can anyone suggest me what is the problem?


    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you Happy Programming! Hari


    Wednesday, November 28, 2012 12:34 PM

Answers

  • I  can't tell form the code that is posted exactly what is causing the problem.  I have concerns with the way the code is organized that the wrong process(s) are being used by the wrong process is being run using the Parallel.ForEach() method.  I would create a list<> to which contains all the running processes like this

    List<ProcessModule> processes = new List<ProcessModule>();


    jdweng

    Wednesday, November 28, 2012 1:37 PM
  • What happens if you execute this line of code

    var processList = new List<ProcessModule>(GetProcessModule());
    and then substitute processList for GetProcessModule() in your switch cases?

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer



    Wednesday, November 28, 2012 2:10 PM

All replies

  • I  can't tell form the code that is posted exactly what is causing the problem.  I have concerns with the way the code is organized that the wrong process(s) are being used by the wrong process is being run using the Parallel.ForEach() method.  I would create a list<> to which contains all the running processes like this

    List<ProcessModule> processes = new List<ProcessModule>();


    jdweng

    Wednesday, November 28, 2012 1:37 PM
  • What happens if you execute this line of code

    var processList = new List<ProcessModule>(GetProcessModule());
    and then substitute processList for GetProcessModule() in your switch cases?

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer



    Wednesday, November 28, 2012 2:10 PM
  • Hi Joel,

    Thanks for the reply. But i have one concern.

    I couldn't able to maintain the list of ProcessModule object, because i will not get all the required ProcessModule objects at a same time. Means first i will get some of the ProcessModule objects, after these ProcessModules got excecuted, then i will get some other ProcessModule objects. i.e second set of ProcessModules will be get excecuted after the first set of ProcessModules got excecuted.

    Please help how can i achieve this..


    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you Happy Programming! Hari

    Friday, November 30, 2012 9:15 AM
  • See code below.  processList is automatically an enumeration.

    ProcessModule process = null;
    var processList = new List<ProcessModule>(GetProcessModule());
    process = _DDMatrix.GetReadyProcess();

    processList.Add(process);


    jdweng

    Friday, November 30, 2012 10:13 AM
  • similar question as been asked regarding check out this

    http://stackoverflow.com/questions/8412879/parallel-foreach-yield-return


    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you Happy Programming!

    Tuesday, December 11, 2012 9:40 AM