-1

I want to make multiple ping using c#, but when i use ping class inside task the value of status not showing.

static void Main(string[] args)
{
    Task.Run(()=> task());
    Console.WriteLine("FINISH");
   

}
public static  async Task task()
{       
    Ping pinger = null;
    pinger = new Ping();         
    var reply = pinger.Send("www.google.om", 1200);
    Console.WriteLine("TEST");
    bool pingable = reply.Status == IPStatus.Success;
    Console.WriteLine(pingable);
}
  • 3
    `await task();` – mjwills Jan 19 '21 at 12:46
  • If you want to repeat something, you don't need a `Task`, you need a loop, like `while` or `for`. – nvoigt Jan 19 '21 at 12:47
  • The `task()` method has no asynchronous calls, so it will execute synchronously. Adding `async` won't make a method run asynchronously by itself. The way this method is written, it would be better to make it a `void task()`. Or a `bool task()` that returns the status – Panagiotis Kanavos Jan 19 '21 at 12:47
  • On the other hand, you could use [SendPingAsync](https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ping.sendpingasync?view=net-5.0) for truly asynchronous execution – Panagiotis Kanavos Jan 19 '21 at 12:50
  • The application closes immediately so how do you expect to see the results? – jdweng Jan 19 '21 at 12:54
  • @jdweng try with console you will see. The apps will show just "FINISH" word. Becouse of that i ask here. My procedure run if without Task. You see on my description i want to create multiple ping. I expected is get value ping without waiting result the first ping (Like DUDE Apps for networking) – Akbar Grunge Jan 19 '21 at 13:05
  • Does this answer your question? [Can't specify the 'async' modifier on the 'Main' method of a console app](https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app) – Theodor Zoulias Jan 19 '21 at 14:15

1 Answers1

1

Use SendPingAsync instead of Send inside another task:

static async Task Main(string[] args)
{
    using(var pinger = new Ping())
    {
        while(someCondition)
        {
            var reply=await pinger.SendPingAsync("www.google.om", 1200);
            var pingable = reply.Status == IPStatus.Success;
            Console.WriteLine(pingable);
            //Wait for 1 second
            await Task.Delay(1000);
        }
    }
    Console.WriteLine("FINISH");
}

Or

static async Task Main(string[] args)
{
    using(var pinger = new Ping())
    {
        while(someCondition)
        {
            await ping(pinger);
            //Wait for 1 second
            await Task.Delay(1000);
        }
    }
    Console.WriteLine("FINISH");
}

static async Task ping(Ping pinger)
{
    var reply=await pinger.SendPingAsync("www.google.om", 1200);
    var pingable = reply.Status == IPStatus.Success;
    Console.WriteLine(pingable);
}

The original code doesn't work because nothing awaits for the task started with Task.Run to complete, so the application terminates immediately. There's no need to use Task.Run though, because Ping has the asynchronous SendPingAsync class

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236