-1

I want to fill the list with more data while the code is running but the window is unresponsive. It worked in MVC application.

private void button1_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            list.Add(textBox1.Text);

            bool pingable = false;
            Ping pinger = new Ping();
            try
            {
                PingReply reply = pinger.Send(textBox1.Text);
                pingable = reply.Status == IPStatus.Success;
            }
            catch (PingException)
            {
                // Discard PingExceptions and return false;
            }
            while (pingable == false)
            {   
                try
                {
                    PingReply reply = pinger.Send(textBox1.Text);
                    pingable = reply.Status == IPStatus.Success;
                }
                catch (PingException)
                {
                    // Discard PingExceptions and return false;
                }

                Thread.Sleep(1000 * 60 * 30);
            }    
            {
                try
                {
                    var mailMsg = new MailMessage();
                    // To
                    mailMsg.To.Add(new MailAddress(textBox2.Text));                    
                    // From
 ect ect

I want to fill out the List with new PC names as the App runs. then i will add a for cycle to do ping for every PC and do pause after emails to the PCs that are online are sent. What am I doing wrong? Or you can't interact with app when event is running?

1 Answers1

1

You can't interact with it while it's running as control won't return to the UI until the function completes.

You need to look into Tasks and/or Async and Await. This is the documentation for Async and Await in c#. And this is a good previous question that has a log of info on Tasks and Async/Await.

Community
  • 1
  • 1
tomRedox
  • 28,092
  • 24
  • 117
  • 154