我想在C#.net中创建一个程序,它将在我的DateTimePicker中的给定时间内执行或运行命令。
示例我想在06:30 PM执行我的命令,现在还不是06:30 PM。程序将等到下午6:30,然后它将执行我的命令。
对不起我只是新手。
你能帮帮我吗?
答案 0 :(得分:2)
您可以使用System.Timers.Timer
每秒运行一次,并检查是否要运行某项任务。像这样举例如:
private System.Timers.Timer _timer;
_timer = new System.Timers.Timer();
_timer.Interval = 1000;
_timer.AutoReset = false; // Fires only once! Has to be restarted explicitly
_timer.Elapsed += MyTimerEvent;
_timer.Start();
private void MyTimerEvent(object source, ElapsedEventArgs e)
{
DateTime now = DateTime.Now;
// Check for tasks to be run at this point of time and run them
...
// Don't forget to restart the timer
_timer.Start();
}
在计时器方法中,您可能需要截断秒数。您还需要确保在您希望它运行的每一分钟内不会每秒运行一次任务。
答案 1 :(得分:0)
您有两种选择: 1)使用任务调度程序(windows - >搜索'任务调度程序')。 2)只需在代码中创建一个计时器,每分钟都会触发一个检查时间的事件。
答案 2 :(得分:0)
您可以使用TaskScheduler类
该链接还提供了一个非常好的使用示例。