Работа с таймером - C# (185615)
Формулировка задачи:
Сделал таймер но он работает существенно медленнее чем должен.
Отображение HH:mm:ss:ff.
Интервал поставил 10 миллисекунд, и каждый раз оно пересчитывает и обновляет текст в textBox.
Я думаю что это из за скорости работы методов, если от этого что-то зависит, то как воплощать это иначе? Или же как усовершенствовать мой вариант?
Вот так я делаю при каждом "тике"
Собстивенно сам класс обработки информации о времени:
private void timer_GameResult_Tick(object sender, EventArgs e)
{
GameTimer.RefreshTime();
tb_TimeResult.Text = GameTimer.CurrentTime;
} public static class GameTimer
{
// Show Current time in Format HH:MM:SS:mm
public static string CurrentTime {get;private set;}
private static int currentMillisecs; // current milliseconds
private static int currentSecs; // current seconds
private static int currentMins; // current minutes
private static int currentHours; // current hours
private static void SetToZero()
{
currentMillisecs = 0;
currentSecs = 0;
currentMins = 0;
currentHours = 0;
}
#region Methods for adding one point to time
// Add 10 milliseconds
private static void AddTenMillisec()
{
if (currentMillisecs == 990) // If it's MAX, add one ses, and set millisecs to zero
{
AddOneSec();
currentMillisecs = 0;
}
else
currentMillisecs += 10;
}
// Same with AddTenMillisec, but with Sec
private static void AddOneSec()
{
if (currentSecs == 59)
{
AddOneMin();
currentSecs = 0;
}
else
currentSecs += 1;
}
// Same with AddTenMillisec, but with Mins
private static void AddOneMin()
{
if (currentMins == 59)
{
AddOneHour();
currentMins = 0;
}
else
currentMins += 1;
}
// Same with AddTenMillisec, but with Hours
private static void AddOneHour()
{
currentHours += 1;
}
#endregion
public static void RefreshTime(bool toZero = false)
{
if (!toZero)
AddTenMillisec();
else
SetToZero();
CurrentTime = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", currentHours, currentMins, currentSecs, currentMillisecs/10);
}
}Решение задачи: «Работа с таймером»
textual
Листинг программы
DateTime time = new DateTime(0, 0);
private void timer1_Tick(object sender, EventArgs e)
{
time = time.AddSeconds(0.1);
label1.Text = time.ToString("mm:ss.ff");
}
private void start_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
timer1.Enabled = false;
else
timer1.Enabled = true;
}