I have a task assistant application created with Windows Merlin character which will remind of few tasks to be done at a perticular time.
I want Merlin to announce the task at a specific predefined time say 10:00 AM which is already set by the user in a small to do list DB.
If i check every minute then it will flood the CPU, if not atleast it will make CPU busy. How to achieve this.
If 开发者_Python百科there are about 10 tasks then it would be hard to compare each and every task.
a check every minute should be no problem at all - your CPU will easily handle this. If you don't want to do this you could create a small Task, that uses Sleep to wait till the next predefined time and then execute your task in another Task/Thread.
This should not use your CPU more than needed.
This could be the (pseudocode) to do this:
void ScheduleNext()
{
var tasks = GetTasksByTimeAscending();
var nextTask = tasks.FirstOrDefault();
if (nextTask == null) return;
System.Threading.Task.Factory.StartNew(() => MyScheduler(nextTask));
}
void MyScheduler(MyTask task)
{
var now = DateTime.Now;
if (task.StartTime > now)
{
var timeToWait = task.StartTime.Substract(now);
System.Threading.Thread.Sleep(timeToWait);
}
System.Threading.Task.Factory.StartNew(task.Action);
ScheduleNext();
}
of course you can insert support for cancellation and error handling if you like.
Couple of ideas, since I'm not aware of all of your constraints:
- Use a scheduler such as Quartz.net
- When a task is created, create a timer that will trigger at right time (i.e. 3 hours from now). That way, you don't have to check... when the task is due, it'll notify you and run whatever code it needs to run.
Hope this gives you enough of an idea to get started.
精彩评论