1
我试图编写一个程序,它打开一个按钮单击窗体。此表单中有倒数的标签。主窗体有一个按钮,它执行以下操作:C#timer_Tick()倒计时2步倒计时
private void btnOpen_Click(object sender, EventArgs e)
{
List<string> ips = new List<string>();
if (pcTreeView.SelectedNodes.Count > 1)
{
foreach (RadTreeNode node in machinesTreeView.SelectedNodes)
{
foreach (XmlNode client in xdoc.SelectNodes("/clients/client"))
{
if (node.Text == client["clientShortName"].InnerText)
{
string ipAddress = client["clientIP"].InnerText;
ips.Add(client["clientIP"].InnerText);
clientNodeList.Add(node);
}
}
}
MsgBox msgbox = new MsgBox();
msgbox.ipAddressCollection = ips;
msgbox.Tag = "test";
msgbox.ShowDialog();
}
}
然后打开第二个窗体。我的倒计时的代码如下:
int timeLeft = 45;
public List<string> ipAddressCollection { get; set; }
private void MsgBox_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private async void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
foreach (string ipAddress in ipAddressCollection)
{
if (this.Tag.ToString() == "test")
{
if (rebootShutdownTime > 0)
{
timeLeft = timeLeft - 1;
infoLabel.Text = "Countdown: " + timeLeft.ToString();
timer1.Enabled = true;
}
}
}
}
的问题是:倒计时在2个步骤进行倒计时(例如20 - 18 - 16等来代替20 - 19 - 18 - 17等)。在调试模式下,它计数正确。
有什么建议吗?
如果您在每次打勾后刷新标签,该怎么办? 'infoLabel.Refresh();' – Montaldo
可能在“发布”中你有2个连接,所以它从timeLeft中减去两次。也许你应该把'timeLeft = timeLeft - 1'移到'for'语句之外。 – dburner
foreach正在运行两次。通过调试来检查它。 – Ricky