H
H
Hirty2016-06-10 19:32:35
C++ / C#
Hirty, 2016-06-10 19:32:35

Pass multiple variables to Parallel.ForEach?

private void button_Click(object sender, RoutedEventArgs e)
        {
                z_prox = Convert.ToInt32(textBox.Text);
                Task.Factory.StartNew(() => {
                testx(accitem, Prox);
            });
        }
        public void testx(string[] accitem, string [] Prox)
        {
            ParallelOptions options = new ParallelOptions();
            options.MaxDegreeOfParallelism = z_prox;
            Parallel.ForEach(Prox, options, Prox1 => {indieroyale(Prox1);});
        }

Tell me how to pass multiple variables to Parallel.ForEach?
Tried the type
Parallel.ForEach(Prox,accitem, options, Prox1 => {indieroyale(Prox1,ck);});

But a mistake

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sanostee, 2016-06-10
@Hirty

/*А нельзя ли просто объединить эти параметры в одну сущность, например так:
class Prox
{
    public string[] Prox1 {get;set;}
    public int[] Prox2 {get;set;}
}*/

If the number of rows in the arrays is equal, try something like this:
var items = new List<KeyValuePair<string, string>>();

for (int i = 0; i < Prox.Length; i++)
{
  items.Add(new KeyValuePair<string, string>(Prox[i], accitem[i]));
}

// с использованием Parallel, если массивы очень огромные
// Parallel.For(0, prox.Length, i => items.Add(new KeyValuePair<string, string>(Prox[i], accitem[i])));

Parallel.ForEach(items, options, p => indieroyale(p.Key, p.Value));

You can replace KeyValuePair with your own class, with two string fields, this does not change the essence.
UPD: And anyway, why do you need Parallel.ForEach? Do everything through Parallel.For, without any joins:
Parallel.For(0, Prox.Length, options, i => indieroyale(Prox[i], accitem[i]));

M
MIsternik, 2016-06-10
@MIsternik

https://msdn.microsoft.com/en-us/library/system.th... here are the answers!

M
MrDywar Pichugin, 2016-06-10
@Dywar

And what prevents to make of two types 1, in which both will be?
As with events, EventArgs 1 pcs, Sender 1 pcs and that's enough.

H
Hirty, 2016-06-11
@Hirty

Simplified a little

ParallelOptions options = new ParallelOptions();
            options.MaxDegreeOfParallelism = Convert.ToInt32(textBox.Text);
            Parallel.ForEach(<b>accitem, Prox,</b> options, (Prox1, ck) => {indieroyale(Prox1, ck); });

But still an error.
Currently, Type Arguments for the Parallel.ForEach(IEnumerable, Func, Func, Action) method cannot be determined by use. Try to explicitly define the type arguments.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question