2017-11-23 2 views
0

술어를 사용하여 TransformBlock에서 ActionBlock으로 전달 된 항목을 필터링 할 때 완료되지 않는 다음 TPL Dataflow가 있습니다.술어를 사용할 때 TPL 데이터 흐름이 완료되지 않음

조건부가 항목에 대해 false를 반환하면 데이터 흐름이 중단됩니다.

누군가가 어떤 일이 일어나고 있는지, 어떻게 해결할 수 있는지에 대한 통찰력을 제공 할 수 있습니까?

// define blocks 
var getBlock = new TransformBlock<int, int>(i => 
{ 
    Console.WriteLine($"getBlock: {i}"); 

    return ++i; 
}); 

var writeBlock = new ActionBlock<int>(i => 
{ 
    Console.WriteLine($"writeBlock: {i}"); 
}); 

// link blocks 
getBlock.LinkTo(writeBlock, new DataflowLinkOptions 
{ 
    PropagateCompletion = true 
}, i => i == 12); // <-- this predicate prevents the completion of writeBlock 

// push to block 
var items = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
foreach (var i in items) 
{ 
    getBlock.Post(i); 
} 

// wait for all operations to complete 
getBlock.Complete(); 
await writeBlock.Completion; // <-- application hangs here 

답변

2

getBlock은 게시 된 항목이 아무 것도 없어서 완료되지 않았습니다. 술어가있는 경우 일치하지 않는 항목이 파이프 라인을 종료 할 장소를 갖도록 null 대상을 추가하십시오.

getBlock.LinkTo(writeBlock, new DataflowLinkOptions 
{ 
    PropagateCompletion = true 
}, i => i == 12) 
getBlock.LinkTo(DataflowBlock.NullTarget<int>()); 
관련 문제