Windows 申请。当代码为 运行 时填写更多数据

Windows application. Fill in more data when code is running

我想在代码为 运行 时用更多数据填充列表,但 window 没有响应。它在 MVC 应用程序中工作。

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

我想在应用程序运行时用新的 PC 名称填写列表。然后我将添加一个 for 循环以对每台 PC 执行 ping,并在向在线 PC 发送电子邮件后暂停。 我究竟做错了什么? 或者当事件为 运行?

时您无法与应用程序交互

当它处于 运行 时您无法与其交互,因为在函数完成之前控件不会 return 到 UI。

您需要查看任务 and/or 异步和等待。 This is the documentation for Async and Await in c#. And this is a good previous question 有关于任务和 Async/Await 的信息日志。