简体   繁体   中英

Parallel.ForEach with conditions

I have list of objects. Object is instance of class. Inside each object is property that hold some value:

List<SomeClassTask> lstSomeTaskList = new List<SomeClassTask>();

for (int i=0; i<5; i++)
{
     SomeClassTask tsk = new SomeClassTask();
     Thread.Sleep(500);
     tsk.SomeProperty = new Random().Next(1, 11);

     lstSomeTaskList.Add(tsk);
}

The next step is to run parallel task for each object

Parallel.ForEach(lstSomeTaskList, task => task.StartProcessing(task.SomeProperty));

I want to start only specific tasks like task which has value 1 (SomeProperty = 1).

This is class:

class SomeClassTask
{
    public int SomeProperty { get; set; }
    public void StartProcessing(int maxRange)
    {
        int rnd = new Random().Next(1, maxRange);
        Thread.Sleep(1000);
        Console.WriteLine("Parallel task -> radnom created value: " + rnd);
    }
}

Is it possible inside

Parallel.ForEach

put some conditions to check some object properties?

Parallel.ForEach Gets a IEnummerable as Source, so you just need to filter your list object and feed it to the ForEach, something like:

SomeClassTask a = new SomeClassTask { SomeProperty = 1 };
SomeClassTask b = new SomeClassTask { SomeProperty = 2 };

List<SomeClassTask> lstSomeTaskList = new List<SomeClassTask> { a, b };

Parallel.ForEach(lstSomeTaskList.Where(task => task.SomeProperty == 1),
task => task.StartProcessing(task.SomeProperty));

Edit
As the comments has pointed out already, you should check this -> https://codeblog.jonskeet.uk/2009/11/04/revisiting-randomness/

You should use Microsoft's Reactive Framework (aka Rx) - NuGet System.Reactive and add using System.Reactive.Linq; - then you can do this:

IObservable<SomeClassTask> query =
    from i in Observable.Range(0, 5)
    let tsk = new SomeClassTask() { SomeProperty = _random.Next(1, 11) }
    where tsk.SomeProperty == 1
    from u in Observable.Start(() => tsk.StartProcessing(tsk.SomeProperty))
    select tsk;

IDisposable subscription = query.Subscribe();

Now, if you want all of the results when the query has finished then do this:

IDisposable subscription =
    query
        .ToList()
        .Subscribe(list => { /* use `list` here */ });

If you need to finish process before it completes then just do subscription.Dispose(); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM