简体   繁体   English

C#如何使用计时器?

[英]C# how to use timer?

I made a student check_list program that's using Bluetooth adapter searches students cell phones Bluetooth and checks that are they present or not and saves students information with date in table on data base.all them works great.But I want to make it automatic that I will put my program on some computer like works as an server and program will make search every lessons start time like 08.30 , 10.25 ... 我制作了一个使用蓝牙适配器的学生check_list程序,可以搜索学生手机的蓝牙并检查它们是否存在,并将学生信息和日期保存在数据库表中。它们都很好用,但是我想使其自动运行将我的程序放在服务器等计算机上,程序将在每课开始时进行搜索,例如08.30,10.25 ...

My question is how to use timer? 我的问题是如何使用计时器? I know how to use timer but How can I use it on every lessons start time?I have table that includes start time of lessons. 我知道如何使用计时器,但如何在每个课程的开始时间使用计时器?我有包含课程开始时间的表格。 Also am I have to stop timer after search ends?And If I stop timer could I re-run timer again? 搜索结束后还必须停止计时器吗?如果我停止计时器,是否可以重新运行计时器?

And one additional question that how can I track that new students come or some body left class room? 还有一个问题,我如何跟踪新来的学生或离开教室的某个身体?

You could periodically check the current time (like every 30 seconds with a simple timer) and if nothing happens, you sleep, if it's 10.25: start your bluetooth polling. 您可以定期检查当前时间(例如使用一个简单的计时器每30秒检查一次),如果什么也没有发生,请睡觉,如果是10.25:开始进行蓝牙轮询。

During class times you could just poll every 5 minutes to see if new students are there. 在上课期间,您可以每5分钟轮询一次,看看是否有新学生在那里。

You could set the timer's Interval property to be the difference between the current time and the time for the next lesson; 您可以将计时器的Interval属性设置为当前时间与下一堂课的时间之间的时差。 then reset the difference after that lesson is finished to be ready for the next. 然后在完成该课程后为下一个课程重置差异。 However, this has obvious pitfalls. 但是,这有明显的陷阱。 What happens when you start/stop the timer? 启动/停止计时器时会发生什么? You will need to reset the Interval for the next lesson. 您将需要为下一课重置时间间隔。

Or, you could make a timer which checks periodically if it is time to recheck the bluetooth devices and if it is time does so. 或者,您可以使计时器定期检查是否需要重新检查蓝牙设备,以及是否需要重新检查蓝牙设备。 It probably wouldn't need to be too accurate. 它可能不需要太准确。

// Add your own DateTimes
DateTime[] times = new[] { new DateTime(2010, 4, 20, 16, 30,0,0), new DateTime(2010, 4, 20, 17, 0,0,0) };

Timer t = new Timer();
t.Interval = 30000; // 30 seconds, feel free to change

// Each 30 secs check to see if the _time_ is before one of the ones specified; if it is RunMethod()
t.Tick += (sender, e) => { if (times.Any(d => { DateTime dt = DateTime.Now; new DateTime(dt.Year, dt.Month, dt.Day, d.Hour, d.Minute, d.Second, d.Millisecond).CompareTo(dt) <= 0 })) RunMethod(); }

我会使用Quartz.NET并安排作业,而不是弄乱计时器...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM