Milan Solanki
Milan Solanki

Reputation: 1207

Threading Parallel Invoke, Action

My code as below

public void DownloadConcurrent(Action<string> Methord)
{
    Action<string>[] methordList = new Action<string>[Concurent_Downloads];

    for (int i = 0; i < Concurent_Downloads; i++)
    {
        methordList[i] = Methord;
    }

    Parallel.Invoke(methordList);
}

Parallel.Invoke is giving error:

"cannot convert from 'System.Action<string>[]' to 'System.Action[]'"

The Method it is calling is

public void DownloadLinks(string Term)
{ 
}

Upvotes: 4

Views: 2670

Answers (3)

Ankur
Ankur

Reputation: 33637

Parallel.Invoke accepts Action array while your code is passing it an Action<string> array. What you can do is :

public void DownloadConcurrent(Action<string> Methord)
{
    Action<string>[] methordList = new Action<string>[Concurent_Downloads];

    var r = methordList.Select(a => (Action)(() => a("some_str"))).ToArray();

    Parallel.Invoke(r);
}

You need to replace some_str with proper value for each action

Upvotes: 1

Dominik
Dominik

Reputation: 3362

check Parallel.ForEach like the following

   static void Main(string[] args)
    {
        List<string> p = new List<string>() { "Test", "Test2", "Test3"};
        Parallel.ForEach(p, Test);
    }


    public static void Test(string test)
    {
        Debug.WriteLine(test);
    }

This should do the trick for you

HTH Dominik

Upvotes: 5

weismat
weismat

Reputation: 7411

In your case it is easier if you use

Parallel.ForEach

over your string list instead of using

Parallel.Invoke

with additional parameter. Let me know if you want to stick to Parallel.Invoke.

Upvotes: 2

Related Questions