CountDownTimer中缺少逻辑 [英] Missing Logic in CountDownTimer

查看:80
本文介绍了CountDownTimer中缺少逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



我为countdowntimer创建了一个小项目,我使用了下面的代码。



- >我通过XML文档获取Hour,Min,Sec的输入。

能够成功运行我的应用程序。



问题:秒计时器运行时间毫秒,分钟运行疯狂。 (这不是以正确的计时器格式运行)



下面的代码,请在错过逻辑的地方纠正我。



private void timer1_Tick(object sender,EventArgs e)

{

if((minutes == 0 )&&(小时== 0)&&(秒== 0))

{

//如果时间结束,请清除所有设置和字段。

//此外,显示消息,通知时间结束。

timer1.Enabled = false;



lblHr.Text =00;

lblMin.Text =00;

lblSec.Text =00;

CloseApps( );



}

else

{

//否则继续计算。

if(秒< 1)

{

秒= 59;

if(minutes == 0)

{

minut es = 59;

如果(小时!= 0)

小时 - = 1;



}

其他

{

分钟 - = 1;

}

}

else

秒 - = 1;

//显示小时,分钟和秒的当前值

/ /相应的字段。

if(分钟< 10)

lblMin.Text =0+ minutes.ToString();

else

lblMin.Text = minutes.ToString() ;



if(小时< 10)

lblHr.Text =0+ hours.ToString();

else

lblHr.Text = hours.ToString();

if(seconds< 10)

lblSec.Text = 0+ seconds.ToString();

else

lblSec.Text = seconds.ToString();

}

}







干杯,

解决方案

你的整个方法太糟糕了,无法考虑特定的问题。首先,它反映了如今许多初学者中非常非常糟糕的趋势:尝试使用代表数据而不是数据本身的字符串。计时器不能以正确的时间格式运行。计时器与格式无关。



您需要丢弃代码,不要再做这些事情,并从头开始。以下是您的需求:



首先,了解时间之间的差异(时间刻度中的一个点由 System.DateTime 和持续时间或间隔,由 System.TimeSpan 表示 - 这是你必须使用的类型: http://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110% 29.aspx [ ^ ]。



现在,您需要确保没有使用计时器类型系统。 Windows.Forms.Timer 。这种类型最容易使用但完全不适合任何应该或多或少周期性的动作,因为这个计时器的时间精度非常低。但即使是其他计时器类型也是如此,你不应该依赖它的周期性行为并像你一样计算事件。相反,在你的计时器的处理程序中t,你应该从系统中获取实时值。在您的情况下,最准确的方法是使用类 System.Diagnostics.Stopwatch

http://msdn.microsoft.com/en-us/library/system.diagnostics。秒表%28v = vs.110%29.aspx [ ^ ]。



从文档中可以看出它的用法;它为您提供 TimeSpan 的值。它允许你计算从计数开始的时间。



只有在完成所有这些简单的计算后,你需要提供你需要显示的时间跨度在你的UI中的字符串。剩下的问题是你的UI通知(太糟糕了,你没有标记你正在使用的)。我假设你正在制作一个Windows应用程序。这是一个小问题:您无法从非UI线程调用与UI相关的任何内容,并且运行timer事件处理程序的线程不是UI线程。您需要使用方法调用 BeginInvoke System.Windows.Threading.Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表格)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()vs。Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何在vb.net中的其他线程上运行keydown事件 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA

Hello Everyone,

Am creating a small project for countdowntimer, i used the below code for the same.

--> Am getting inputs for Hour, Min, Sec via a XML document.
Am able to run my application successfully.

Issue: Seconds timer run like milliseconds and Minutes run crazy. (This is not running in a correct timer format)

The Code Below, please correct me where am missing the logic.

private void timer1_Tick(object sender, EventArgs e)
{
if ((minutes == 0) && (hours == 0) && (seconds == 0))
{
// If the time is over, clear all settings and fields.
// Also, show the message, notifying that the time is over.
timer1.Enabled = false;

lblHr.Text = "00";
lblMin.Text = "00";
lblSec.Text = "00";
CloseApps();

}
else
{
// Else continue counting.
if (seconds < 1)
{
seconds = 59;
if (minutes == 0)
{
minutes = 59;
if (hours != 0)
hours -= 1;

}
else
{
minutes -= 1;
}
}
else
seconds -= 1;
// Display the current values of hours, minutes and seconds in
// the corresponding fields.
if (minutes < 10)
lblMin.Text = "0" + minutes.ToString();
else
lblMin.Text = minutes.ToString();

if (hours < 10)
lblHr.Text = "0" + hours.ToString();
else
lblHr.Text = hours.ToString();
if (seconds < 10)
lblSec.Text = "0" + seconds.ToString();
else
lblSec.Text = seconds.ToString();
}
}



Cheers,

解决方案

Your whole approach is too bad to consider particular problems. First of all, it reflects the very, very bad trend notorious in many beginners these days: attempting to work with strings representing data instead of data itself. The timer cannot be "running in a correct time format". Timer has nothing to do with the format.

You need to throw out your code, never do such things again, and start from scratch. Here is what you need:

First, understand the difference between time (a point in a time scale expressed by System.DateTime and time duration or interval, expressed by System.TimeSpan — this is the type you have to use: http://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx[^].

Now, you need to make sure you are not using the timer type System.Windows.Forms.Timer. This type is the easiest to use but totally unsuitable for any action supposed to be more or less periodic, due to prohibitively low time accuracy of this timer. But even with other timer types, you should not rely on its periodic behavior and count the events as you do. Instead, in the handler of your timer event, you should take the real time value from the system. In your case, the most accurate way of doing so is using the class System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx[^].

Its usage is apparent from the documentation; it give you the values of TimeSpan. It allows you to calculate the time from start of the counting.

And only after all these simple calculations are done, you need to present the time span you need to show in you UI in a string. The remaining problem is the notification of your UI (too bad you did not tag what you are using). Let me assume you are making a windows application. Here is a little problem: you cannot call anything related to UI from non-UI thread, and the thread running the handler of your timer event is not the UI thread. You need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于CountDownTimer中缺少逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆