我有這個類:如何在Action中傳遞字符串參數?
public class PCQueue : IDisposable
{
public delegate void OnFileAddDelegate(string file);
public event OnFileAddDelegate OnFileAddEventHandler;
BlockingCollection<Action> _taskQ = new BlockingCollection<Action>();
public PCQueue (int workerCount)
{
// Create and start a separate Task for each consumer:
for (int i = 0; i < workerCount; i++)
Task.Factory.StartNew (Consume);
}
public void Dispose() { _taskQ.CompleteAdding(); }
public void EnqueueTask (Action action) { _taskQ.Add (action); }
void Consume()
{
// This sequence that we’re enumerating will block when no elements
// are available and will end when CompleteAdding is called.
foreach (Action action in _taskQ.GetConsumingEnumerable())
action(); // Perform task.
}
}
,我有string[]
,我想添加到我的Queue
之後,我想我的Consume()
得到爲了處理這個文件。
string[] files;
PCQueue pq = new PCQueue(1);
foreach (string item in files)
pq.EnqueueTask(item);
我有這樣的錯誤:無法從「字符串」到「System.Action」
轉換後,我把文件從我Queue
我是檢查這個文件:
Checker checker = new Checker();
string result = Checker.Check(my file from the Queue);
if (result != null && OnFileAddEventHandler != null)
OnFileAddEventHandler(result);
如果這個文件是好的我激發了事件到我的表格
當然,隊列應該包含數據,而不是任務? – 2014-08-29 08:26:16
@MthetheWWatson Nope,似乎整個過程是用預定義數量的工作人員處理文件的集合,所以無論是處理「Action」都存儲在隊列中,還是作爲「Action」的隊列。 –
AndreySarafanov
2014-08-29 08:46:20
@AndreySarafanov在這種情況下,您只需創建一個任務數組並使用Parallel.Invoke()來啓動它們,併爲工作項使用BlockingCollection。 – 2014-08-29 09:07:20