在Azure中使用特定时间的计时器

本文关键字:定时间 计时器 Azure | 更新日期: 2023-09-27 18:19:22

我有我的工作者角色,我需要在每天上午10点运行一个特定的任务。

public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {                  
        while (true)
        {
            if (DateTime.Now.Hour == 10)
            {
                //Do Specific timer Job();
            }
            //Do Normal Worker process();
            Thread.Sleep(TimeSpan.FromMinutes(1));
        }
    }

这将多次运行计时器作业,因为我只得到一个小时,我不能用一个特定的时间来检查它,比如10.00,因为有可能它会被主工作进程跳过。

我需要知道实现这个的理想方法。

我已经看到了这个链接和那些在答案中提到的,

如何在windows azure worker角色中调度任务

但是我需要一个不使用azure调度器或任何第三方工具的解决方案:(无论如何,我可以使用计时器来检查特定的时间?

在Azure中使用特定时间的计时器

也许你可以使用Azure WebJobs

http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/调度器

还有,Scott Hanselman今天的帖子与包括Quartz在内的各种库。

http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx

插入一个简单的队列消息并设置该消息的可见性超时。这样,消息将只在那个时候可见,您可以执行操作。

public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {                  
        while (true)
        {
            CloudQueueMessage queuemessage = queuestorage.GetMessage(queue name)
            if(queueMessage.exist && queueMessage insertion time < 10 am today)
            {
                if (DateTime.Now.Hour >= 10)
                {
                    //Do Specific timer Job();
                    //Delete the queue message and insert new one for the task next day.
                }
            }
            //Do Normal Worker process();
            Thread.Sleep(TimeSpan.FromMinutes(1));
        }
    }
}