开发者

Threading Parallel Invoke, Action

开发者 https://www.devze.com 2023-03-16 16:43 出处:网络
My code as below public void DownloadConcurrent(Action<string> Methord) { Action<string>[] methordList = new Action<string>[Concurent_Downloads];

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 con开发者_如何学Govert from 'System.Action<string>[]' to 'System.Action[]'"

The Method it is calling is

public void DownloadLinks(string Term)
{ 
}


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消