我有一个间隔为1的计时器。每当它打勾时,我想添加时间并将其显示在我的表单上。使用计时器以秒为单位的显示时间
但它有什么不对。时间更新本身的方式来减慢。如果我将间隔设置为1000,它可以工作,但我需要它运行得更快。
这里是我的代码:
private void button1_Click_1(object sender, EventArgs e)
{
timer.Interval = 1;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
lCount++;
label1.Text = GetTimeForGUI(lCount);
}
private String GetTimeForGUI(long lTimeInMilliSeconds)
{
String sGUITime = string.Empty;
try
{
// Get Format: 00:00
if (((lTimeInMilliSeconds/1000) % 60) == 0)
{
sGUITime = Convert.ToString((lTimeInMilliSeconds/1000)/60);
// Get the number of digits
switch (sGUITime.Length)
{
case 0:
sGUITime = "00:00";
break;
case 1:
sGUITime = "0" + sGUITime + ":" + "00";
break;
case 2:
sGUITime = sGUITime + ":" + "00";
break;
}
}
else
{
long lMinutes;
long lSeconds;
// Get seconds
lTimeInMilliSeconds = lTimeInMilliSeconds/1000;
lSeconds = lTimeInMilliSeconds % 60;
lMinutes = (lTimeInMilliSeconds - lSeconds)/60;
switch (Convert.ToString(lMinutes).Length)
{
case 0:
sGUITime = "00";
break;
case 1:
sGUITime = "0" + Convert.ToString(lMinutes);
break;
case 2:
sGUITime = Convert.ToString(lMinutes);
break;
}
sGUITime = sGUITime + ":";
switch (Convert.ToString(iSeconds).Length)
{
case 0:
sGUITime = sGUITime + "00";
break;
case 1:
sGUITime = sGUITime + "0" + Convert.ToString(lSeconds);
break;
case 2:
sGUITime = sGUITime + Convert.ToString(lSeconds);
break;
}
}
return sGUITime;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return string.Empty;
}
}
这难道不是问题吗? if(((lTimeInMilliSeconds/1000)%60)== 0) –
它什么时候更新很慢,间隔为1或1000?这是有点不清楚你问的。 – Abbas
请告诉我们你正在尝试做什么?你想达到什么目的? –