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 8851337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:07:52+00:00 2026-06-14T13:07:52+00:00

This question is a follow-up to a previous question that I had asked: How

  • 0

This question is a follow-up to a previous question that I had asked:

How to Perform Multiple "Pings" in Parallel using C#

I was able to get the accepted answer (a Windows console application) to work, but when I tried to run the code in a Windows forms application, the following code will freeze on the line containing Task.WaitAll(pingTasks.ToArray()). Here is the code that I am trying to run:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            List<String> addresses = new List<string>();

            for (Int32 i = 0; i < 10; ++i) addresses.Add("microsoft.com");

            List<Task<PingReply>> pingTasks = new List<Task<PingReply>>();
            foreach (var address in addresses)
            {
                pingTasks.Add(PingAsync(address));
            }

            //Wait for all the tasks to complete
            Task.WaitAll(pingTasks.ToArray());

            //Now you can iterate over your list of pingTasks
            foreach (var pingTask in pingTasks)
            {
                //pingTask.Result is whatever type T was declared in PingAsync
                textBox1.Text += Convert.ToString(pingTask.Result.RoundtripTime) + Environment.NewLine;

            }

        }

        private Task<PingReply> PingAsync(string address)
        {
            var tcs = new TaskCompletionSource<PingReply>();
            Ping ping = new Ping();
            ping.PingCompleted += (obj, sender) =>
            {
                tcs.SetResult(sender.Reply);
            };
            ping.SendAsync(address, new object());
            return tcs.Task;
        }

    }

}

Does anyone have any ideas as to why it’s freezing?

  • 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-14T13:07:53+00:00Added an answer on June 14, 2026 at 1:07 pm

    It’s freezing because WaitAll waits on all of the tasks, and you’re in the UI thread, so that’s blocking the UI thread. Blocking the UI thread freezes your application.

    What you want to do, since you’re in C# 5.0, is await Task.WhenAll(...) instead. (You’ll also need to mark that event handler as async in it’s definition.) You won’t need to change any other aspects of the code. That will work just fine.

    await won’t actually “wait” in the tasks. What it will do is, when it hits the await, it will wire up a continuation to the task you are awaiting on (in this case, the when all) and in that continuation it will run the remainder of the method. Then, after wiring up that continuation, it will end the method and return to the caller. This means that the UI thread isn’t blocked, since this click event will end right away.

    (Upon request) If you want to solve this using C# 4.0 then we’ll need to start by writing WhenAll from scratch, since it was added in 5.0. Here is what I just whipped up. It’s probably not quite as efficient as the library implementation, but it should work.

    public static Task WhenAll(IEnumerable<Task> tasks)
    {
        var tcs = new TaskCompletionSource<object>();
        List<Task> taskList = tasks.ToList();
    
        int remainingTasks = taskList.Count;
    
        foreach (Task t in taskList)
        {
            t.ContinueWith(_ =>
            {
                if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else if (t.IsFaulted)
                {
                    tcs.TrySetException(t.Exception);
                }
                else //competed successfully
                {
                    if (Interlocked.Decrement(ref remainingTasks) == 0)
                        tcs.TrySetResult(null);
                }
            });
        }
    
        return tcs.Task;
    }
    

    Here is another option based on this suggestion in the comments by svick.

    public static Task WhenAll(IEnumerable<Task> tasks)
    {
        return Task.Factory.ContinueWhenAll(tasks.ToArray(), _ => { });
    }
    

    Now that we have WhenAll we just need to use that, as well as continuations, instead of await. Instead of WaitAll you’ll use:

    MyClass.WhenAll(pingTasks)
        .ContinueWith(t =>
        {
            foreach (var pingTask in pingTasks)
            {
                //pingTask.Result is whatever type T was declared in PingAsync
                textBox1.Text += Convert.ToString(pingTask.Result.RoundtripTime) + Environment.NewLine;
            }
        }, CancellationToken.None,
        TaskContinuationOptions.None,
        //this is so that it runs in the UI thread, which we need
        TaskScheduler.FromCurrentSynchronizationContext());
    

    Now you see why the 5.0 option is prettier, and this is a reasonably simple use case too.

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

Sidebar

Related Questions

This is a follow up to a previous question that I had before about
So this is a follow-up to a previous question that I asked: Trying to
This is a follow up question to a previous question I asked about calculating
This is a follow-up on a previous question I had ( Complexity of STL
This is a follow-on from a previous question I had: How to decouple my
This question is a follow-up question to my previous one that can be found
This is a follow up to a previous question I asked How to encode
Note that this is a follow-up question from my previous one: How to memorize
This is a follow-up to a previous question I had about interfaces. I received
This is a follow-up to a previous question where I had a trait Garage

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.