调用对象'特定时间的方法C#

时间:2015-06-29 19:08:13

标签: c# scheduled-tasks

我有点困惑,正在寻求建议。

我想做什么的想法:

Program started 
foreach file in directory 'tasks'
   Deserialize file to object
   DateTime date = GetExcecutionDate()
   if (Date.Now.DateDiff(date) < 0)
        async object.Excecute()
   else
        //Set task for file to execute at date
End foreach
Programs continue executing


When Task is called, I need to know filename
async
    Deserialize file to object
    object.Execute();
    ReloadTask(filename); //Sets new fire time for this file 

实施此机制的最佳方法是什么。我需要的是如何强制程序调用特定方法,该方法将在特定时间接收字符串。我应该以哪种方式挖掘 - 定时器或者讲道者或其他东西?

1 个答案:

答案 0 :(得分:0)

You could get the next time it will run and subtract now() and set a timer to run your actions:

TimeSpan timeSpan = date.Subtract(DateTime.Now.AddSeconds(1));
double interval = timeSpan.TotalMilliseconds;
Timer _timer = new Timer();
_timer.Interval = interval;
_timer.Elapsed += PerformAction ;
_timer.AutoReset = false;
_timer.Start();

with PerformAction somewhere:

public void PerformAction (object sender, ElapsedEventArgs e)