【发布时间】:2013-11-22 11:31:31
【问题描述】:
我有一个间隔为 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?有点不清楚你在问什么。
-
告诉我们您到底想要做什么?你想完成什么?
-
Uaaa 停止使用那个匈牙利符号……
-
对于线程安全增量使用 Interlocked.Increment(lCount) 几乎 lCount++;