I
I
itgood2021-05-27 11:27:49
C++ / C#
itgood, 2021-05-27 11:27:49

What is the correct way to Wait for the completion of a task wait?

i have an asynchronous method a method that checks the port and returns the result and there is a method that starts the check and there is a condition that displays the result when you run many methods it comes out like this 60af576c2ce90914756016.pngif the method did not have time to return the status before starting a new task we will not know the result, how can we bypass it?
60af5801d52d4084824598.png

private async void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            panel = new StatusPanel();
            ProgressBar = new ProgressBar();
     
            panel.TypePortBlock.Content = ProgressBar;
            PaneStat.Children.Add(panel);
            var obj = (CheckBox)sender;
           
            string[] Ip = obj.Content.ToString().Split(new char[] { '/' });

            panel.Name = RandomString(random.Next(1,30));

            panel.IpaddressBlock.Text = GetIPAddress() ;
            panel.PortBlock.Text = Ip[0];


            ProgressBar.IsIndeterminate = true;
            ProgressBar.Width = 40;
            ProgressBar.Height = 10;
            ProgressBar.Maximum = 100;
            ProgressBar.Minimum = 0;
            
            panel.StatusBlock.Background = Brushes.Orange;
            panel.StatusBlock.Text = "Trying to connect";

            if (await Task.Run(() => StatusPort(int.Parse(Ip[0]))) == true)
            {

                panel.StatusBlock.Background = Brushes.Green;
                panel.StatusBlock.Text = "Enabled";
                ProgressBar.Value = 100;
                ProgressBar.IsIndeterminate = false;
      

            }
            else
            {
                panel.StatusBlock.Background = Brushes.Red;
                panel.StatusBlock.Text = "disabled";
                ProgressBar.Value = 100;
                ProgressBar.IsIndeterminate = false;




            }

        }
        private async Task<bool> StatusPort(int port)
        {
            bool status=false;
            try
            {
                TcpClient t = new TcpClient(AddressFamily.InterNetwork);
             

                IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.222.17"), port);
            //   t.SendTimeout = 5;
             
                t.Connect(ipEndPoint);
                status = true;


            }
             catch (SocketException ex)
            {
               
                status =false;
            }

            return status;
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2021-05-27
@itgood

The status check method is launched and immediately puts the result working, the check method if it sees the status I work falls asleep when there is data, the status is set to ready. If the status check method is launched and the status is not equal to ready, then it is simply interrupted, you still need to add error checking and a timeout

P
Peter, 2021-05-27
@petermzg

Waiting for an asynchronous method to complete is generally not correct.
It is more correct to inform an asynchronous method about its completion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question