Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:46:09+00:00 2026-06-17T20:46:09+00:00

Using TPL, how do I collect results from multiple IO sources (thread-less tasks) and

  • 0

Using TPL, how do I collect results from multiple IO sources (“thread-less” tasks) and merge them into a sequence as they come in from their respective sources without spawning a thread based task per source to monitor them? Would it be safe to poll the sources from one thread?

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

How about something like that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:46:11+00:00Added an answer on June 17, 2026 at 8:46 pm

    I see several options here:

    If you want fire up the calls to ReceiveAsync(), set them up to do something with the result (e.g. send to a dataflow block, like you said) and then forget about them, you could use ContinueWith():

    foreach (var channel in readyChannels)
    {
       channel.ReceiveAsync().ContinueWith(task => 
       {
           var result = task.Result;
           //do something with result like post to dataflow block.
       }
    }
    

    One disadvantage of this is that you would need to handle exceptions in each continuation.

    Probably a better approach is to use OrderByCompletion() from Stephen Cleary’s AsyncEx. This way, you can start all reads at once and process them as they complete:

    var tasks = readyChannels.Select(c => c.ReceiveAsync()).OrderByCompletion();
    
    foreach (var task in tasks)
    {
       var result = await task;
       //do something with result like post to dataflow block.
    }
    

    Yet another option, useful for example if you want to limit parallelism, is to use TransformBlock:

    var receiveBlock = new TransformBlock<UdpClient, UdpReceiveResult>(
        c => c.ReceiveAsync(),
        new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = degreeOfParallelism });
    foreach (var channel in readyChannels)
        receiveBlock.Post(channel);
    receiveBlock.Complete();
    
    // set up processing here
    
    await receiveBlock.Completion;
    

    If you want to send the results to another block, then the processing mentioned in a comment above consists of simply linking them together:

    receiveBlock.LinkTo(anotherBlock);
    

    In all of the cases above, there is never a thread blocking to monitor anything. But the code to call ReceiveAsync() and then to process the result has to execute somewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the TPL to add new tasks to the system thread pool
I am using TPL to perform two tasks serially on a background thread in
What is the difference between: Starting a new thread Using TPL Using BackgroundWorker All
Thanks for your time and assistance in advance. I am using TPL from WPF
Possible Duplicate: How to create a task (TPL) running a STA thread? I'm using
What does this mean and how to resolve it? I am using TPL tasks.
I'm using Drupal 7. My bg image in page.tpl.php file and image get to
Using a populated Table Type as the source for a TSQL-Merge. I want to
In particular, I'm looking at using TPL to start (and wait for) external processes.
Given the following array Func<int>[] funcs and using TPL (Task Parallel Library .NET) how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.