async-await Returns control to the first method

Viewed 28

i have a strange problem in using async/await

i have there methods(nested) that their execution need time,so i using async awit keyword, and there is no error. i have a logical error, that i cant found the reasen that.

now just be for clear, i created an image to show what happen in fact :

async/await returns control to wrong method

i expect the third method after execute respond to second caller. but the third method after run returns control to the first method!!!

here is my code :

  var netStatus = NetworkTools.CheckLocalNetworkAsync();
            iconStatusTitle.IconChar = FontAwesome.Sharp.IconChar.CircleInfo;
            var run = await netStatus;

second caller code :

        public static async Task<List<IPAndDNS>> CheckLocalNetworkAsync()
    {
        var ipAndDnsJustInCase = new IPAndDNS();
        var listIPAndDNS = new List<IPAndDNS>();
        var checkIPv4List = new List<IPAddress>();
        var host = Dns.GetHostEntry(Dns.GetHostName());

        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                if ((!ip.ToString().StartsWith("169")) && (ip.ToString() != "127.0.0.1"))
                {
                    checkIPv4List.Add(ip);
                }
                else if (ip.ToString() == "127.0.0.1")
                {
                    ipAndDnsJustInCase.NetworkStatus = IPAndDNS.NetStatus.Loopback;
                    listIPAndDNS.Add(ipAndDnsJustInCase);
                    return listIPAndDNS;
                }
                else if (ip.ToString().StartsWith("169"))
                {
                    ipAndDnsJustInCase.NetworkStatus = IPAndDNS.NetStatus.DHCPNotInAccess;
                    listIPAndDNS.Add(ipAndDnsJustInCase);
                    return listIPAndDNS;
                }
                else
                    throw new Exception("Network Status Is Uknown");
            }
            else
            {
                continue;
            }
        }

        var ipAndDns = CheckLocalNetworkAddress(checkIPv4List);

        foreach (var item in ipAndDns)
        {
            item.GatewayInAccess = await CheckGatewayAddressAsync(item.DefaultGateway);
            if (item.GatewayInAccess)
            {
                item.NetworkStatus = IPAndDNS.NetStatus.HasIp;
            }
        }

        return ipAndDns;
    }

and third caller :

        public static async Task<bool> CheckGatewayAddressAsync(string ip)
    {
        // Create Ping instance.
        Ping ping = new Ping();

        //This code ensures that the method is executed asynchronously   
       // await Task.Delay(1000);
        PingReply reply = null;
        
            reply = await ping.SendPingAsync(ip);
        
        // Send a ping.
        //PingReply reply = await ping.SendPingAsync(GetGateway());
        // Display the result.

        if (reply != null && reply.Status == IPStatus.Success)
        {
            ping.Dispose();
            return true;
        }
        else
        {
            ping.Dispose();
            return false;
        }
    }

this is last method that has wrong control return to wrong method.

thank's for any help

0 Answers
Related