没有do循环,我的代码运行正常。只要我将它放在do或while循环中,代码就无法更新颜色状态。任何的想法?根据我从互联网上收集的内容,我的循环写得正确。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Threading;
namespace SystemsUpDown
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
bool ContinuePing = false;
private void QuitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void StartButton_Click_1(object sender, EventArgs e)
{
Ping ping = new Ping();
ContinuePing = true;
do
{
try ///ping google
{
PingReply reply = ping.Send("8.8.8.8");
if (reply.Status == IPStatus.Success)
{
GoogleStatusLabel.BackColor = Color.Green;
}
}
catch
{
GoogleStatusLabel.BackColor = Color.Red;
}
try ///ping Yahoo!
{
PingReply reply = ping.Send("www.yahoo.com");
if (reply.Status == IPStatus.Success)
{
YahooStatusLabel.BackColor = Color.Green;
}
}
catch
{
YahooStatusLabel.BackColor = Color.Red;
}
try ///ping Reddit.com
{
PingReply reply = ping.Send("www.reddit.com");
if (reply.Status == IPStatus.Success)
{
RedditStatusLabel.BackColor = Color.Green;
}
}
catch
{
RedditStatusLabel.BackColor = Color.Red;
}
try ///ping Chive
{
PingReply reply = ping.Send("www.chive.com");
if (reply.Status == IPStatus.Success)
{
ChiveStatusLabel.BackColor = Color.Green;
}
}
catch
{
ChiveStatusLabel.BackColor = Color.Red;
}
try ///ping CNN
{
PingReply reply = ping.Send("www.cnn.com");
if (reply.Status == IPStatus.Success)
{
CNNStatusLabel.BackColor = Color.Green;
}
}
catch
{
CNNStatusLabel.BackColor = Color.Red;
}
} while (ContinuePing);
}
private void StopButton_Click(object sender, EventArgs e)
{
GoogleStatusLabel.BackColor = Color.Yellow;
ChiveStatusLabel.BackColor = Color.Yellow;
CNNStatusLabel.BackColor = Color.Yellow;
RedditStatusLabel.BackColor = Color.Yellow;
YahooStatusLabel.BackColor = Color.Yellow;
ContinuePing = false;
}
}
}
答案 0 :(得分:1)
更改颜色后尝试强制刷新标签:
GoogleStatusLabel.Refresh();
答案 1 :(得分:1)
这可能是因为循环运行得足够快,因此更新无法理解。
尝试在循环中添加一些睡眠,如
//Sleep for two seconds. You can add this at end of loop.
//Or, sleep for 2 secs after pinging one site.
// 2000 miliseconds = 2 seconds.
System.Threading.Thread.Sleep(2000);
然后检查。
我建议您使用 BackgroundWorker 。因为主线程中的while循环将挂起主窗口窗体。
BackgroundWorker 适用于对窗体控件进行此类定期更新,以便主窗口不会挂起。 请参考以下链接 - https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
答案 2 :(得分:0)
您的进程在与UI线程不同的线程上持续运行,没有时间更新UI项目。
使用Update / Refresh强制UI更新一些时间,或使用sleep让UI线程有一些时间
答案 3 :(得分:0)
使用[
["a",1,4,1],
["b",2],
["c",3,3,2],
["d",2],
["x",5,1],
["f",3],
["o",7]
]
标记Click()处理程序,然后将循环放入带有匿名委托的Dim Consulta As New OleDb.OleDbDataAdapter("Select m.idMedicos, m.Nombre, m.Apellido, h.idHistorial, h.CedulaP, r.*, rm.*, me.Nombre, me.Tipo, me.Administracion FROM (((Medicos m INNER JOIN Historial h ON m.idMedicos = h.idMedicos ) INNER JOIN Recetas r ON r.idHistorial = h.idHistorial ) INNER JOIN RecetaMedica rm ON rm.idReceta = r.idReceta ) INNER JOIN Medicamentos me ON me.idMedicamento = rm.idMedicamento WHERE h.CedulaP ='" + dato + "'", conexion)
。使用async
更新用户界面,我在下面使用辅助方法完成了该操作:
await Task.Run()