1
            var tasks0 = new BlockingCollection<object>(new ConcurrentQueue<object>());
            tasks0.Add(Fetch(APCounter));
            tasks0.Add(Decode(APCounter));
            tasks0.Add(ALURes(APCounter));
            tasks0.Add(Memory(APCounter));
            tasks0.Add(WriteB(APCounter));


            var tasks1 = new BlockingCollection<object>(new ConcurrentQueue<object>());
            tasks1.Add(Fetch(APCounter+1));
            tasks1.Add(Decode(APCounter + 1));
            tasks1.Add(ALURes(APCounter + 1));
            tasks1.Add(Memory(APCounter + 1));
            tasks1.Add(WriteB(APCounter + 1));

我不希望它执行现在添加的功能。稍后我将使用业务逻辑手动执行此操作。!!!

4

3 回答 3

1

如果我正确理解您想要存储在函数队列列表中的问题,稍后将调用该问题。如果是这种情况,您需要的是:

tasks0.Add(() => Fetch(APCounter)); 
tasks0.Add(() => Decode(APCounter)); 

等等 - 因为

tasks0.Add(Fetch(APCounter)); 

将调用的结果添加到Fetch(APCounter),而

tasks0.Add(() => Fetch(APCounter)); 

添加一个委托调用Fetch(APCounter)

于 2012-05-01T17:37:23.727 回答
1

如果您尝试这样做,则不会将方法添加到列表中。您正在调用类似的方法Fetch(APCounter),然后将该方法的结果添加到BlockingCollection.

尝试创建委托并将委托添加到集合中。

public delegate FetchFunction(int apCounter);
tasks0.Add(FetchFunction(APCounter);

// or using lambda expressions
tasks0.Add(() => Fetch(APCounter));
于 2012-05-01T17:39:26.613 回答
1

正如其他人所说,您需要存储代表列表或类似的东西,才能做您想做的事。您正在执行这些函数,然后将结果存储在阻塞集合中。您需要做的第一件事是将阻塞集合上的泛型类型更改为:

//add tasks
//if the functions return a common object other than "object" then adjust this accordingly
var tasks0 = new BlockingCollection<Func<object>>();//defaults to using a ConcurrentQueue
tasks0.Add(()=>Fetch(APCounter));
tasks0.Add(()=>Decode(APCounter));
tasks0.Add(()=>ALURes(APCounter));
tasks0.Add(()=>Memory(APCounter));
tasks0.Add()=>(WriteB(APCounter));

然后实际执行功能:

//run tasks
object firstResult = tasks0.Take()();
object secondResult = tasks0.Take()();
//...
于 2012-05-01T18:05:19.177 回答