简体   繁体   English

发生PingException mscorlib.dll

[英]PingException occurred mscorlib.dll

I'm trying to read ip address from .csv file and ping those addresses using PingAsync class. 我正在尝试从.csv文件读取IP地址,并使用PingAsync类ping这些地址。 But I'm getting above exception in the following line. 但是我在下面的行中超越了异常。

await Task.WhenAll(pingTasks);

my full code is Given below please take a look. 我的完整代码如下,请看一看。

Ping Method 平方法

private static async Task AsyncPingTask(List<string> ipaddress)
{
    try
    {
        Console.WriteLine("Ping Started");
        StringBuilder pingStringBuilder = new StringBuilder();


            var pingTasks = ipaddress.Select(ip =>
            {
                using (var ping = new Ping())
                {
                    return ping.SendPingAsync(ip);
                }
            }).ToList();

            Console.WriteLine("Ping Completed");

            await Task.WhenAll(pingTasks);


            foreach (var pingReply in pingTasks)
            {
                pingStringBuilder.Append(pingReply.Result.Address);
                pingStringBuilder.Append("-->");
                pingStringBuilder.Append(pingReply.Result.Status);
                pingStringBuilder.Append("-->");
                pingStringBuilder.Append(pingReply.Result.RoundtripTime.ToString());
                pingStringBuilder.AppendLine();
            }
            Console.WriteLine(pingStringBuilder.ToString());
            pingStringBuilder.Clear();                

    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message.ToString());
        throw;
    }

    }

Main Method: 主要方法:

public static void Main()
{
    List<string> address = new List<string>();
    Task t = Task.Run(() =>
    {
        var reader = new StreamReader(File.OpenRead(Environment.CurrentDirectory+@"\address.csv"));
        while (!reader.EndOfStream)
        {
            var lines = reader.ReadLine();
            var values = lines.Split(';');
            address.Add(values[0]);
        }                
    });


    Console.WriteLine("List COunt is {0}",address.Count);
    Stopwatch timeSpan=Stopwatch.StartNew();


    t.Wait();            


    AsyncPingTask(address).Wait();

    Console.WriteLine(timeSpan.ElapsedMilliseconds);                        
    Console.ReadLine();
}

If I done any mistake please guide me. 如果我有任何错误,请指导我。

This piece of code is the problem: 这段代码就是问题所在:

var pingTasks = ipaddress.Select(ip =>
{
    using (var ping = new Ping())
    {
        return ping.SendPingAsync(ip);
    }
}).ToList();

You're disposing the Ping class before the async operation has a chance to complete, since you're not awaiting on SendPingAsync , but simply returning the hot task it produces. 您要在async操作有机会完成之前处理Ping类,因为您不是在等待SendPingAsync ,而只是返回它产生的热任务。 This leads the the disposal prior to the completion of the operation. 这导致在完成操作之前进行处置。

Change your code to: 将您的代码更改为:

var pingTasks = ipaddress.Select(ip =>
{
    var ping = new Ping();
    return ping.SendPingAsync(ip);
}).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在mscorlib.dll中使用list &lt;&gt;发生StackOverflowException - StackOverflowException occurred in mscorlib.dll with list<> mscorlib.dll 中出现“System.StackOverflowException”? - 'System.StackOverflowException' occurred in mscorlib.dll? mscorlib.dll中发生'System.ArgumentOutOfRangeException' - 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll mscorlib.dll中发生了类型为&#39;System.InvalidOperationException&#39;的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll中发生了未处理的“System.StackOverflowException”类型异常 - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll mscorlib.dll 中出现奇怪的错误“System.ExecutionEngineException” - Weird error 'System.ExecutionEngineException' occurred in mscorlib.dll mscorlib.dll C#中发生System.ArgumentOutOfRangeException - System.ArgumentOutOfRangeException occurred in mscorlib.dll C# mscorlib.dll中发生“ System.FormatException”,但未在用户代码中处理 - 'System.FormatException' occurred in mscorlib.dll but was not handled in user code mscorlib.dll中发生了未处理的“System.FormatException”类型异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM