使用 TPL,我如何从多个 IO 源(“无线程”任务)收集结果并将它们合并到一个序列中,因为它们从各自的源进来,而不为每个源生成一个基于线程的任务来监视它们?从一个线程轮询源是否安全?
while (true)
{
try
{
IEnumerable<UdpClient> readyChannels =
from channel in channels
where channel.Available > 0
select channel;
foreach( UdpClient channel in readyChannels)
{
var result = await channel.ReceiveAsync();
//do something with result like post to dataflow block.
}
}
catch (Exception e)
{
throw (e);
}
...
那样的事情怎么样?