2011-06-30 24 views
4

我的代碼如下線程並行調用,操作

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是給錯誤:

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

它調用方法是

public void DownloadLinks(string Term) 
{ 
} 

回答

5

檢查Parallel.ForEach像下面

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); 
    } 

這應該爲你

HTH 多米尼克

2

在你如果您使用

Parallel.ForEach

在你的字符串列表,而不是使用

Parallel.Invoke

額外的參數。讓我知道如果你想堅持Parallel.Invoke。

1

Parallel.Invoke做的伎倆接受Action陣列,同時在你的代碼是傳遞一個Action<string>陣列。你可以做的是:

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); 
} 

你需要適當的值來代替some_str每個動作