简体   繁体   中英

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 ...

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.

During class times you could just poll every 5 minutes to see if new students are there.

You could set the timer's Interval property to be the difference between the current time and the time for the next lesson; 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并安排作业,而不是弄乱计时器...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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