简体   繁体   English

在托管服务器C#中定期安排任务

[英]Schedule a Task periodically in a hosted server c#

I have a requirement communicating with ftp server after a certain time interval and host it in a cloud. 我要求在一定时间间隔后与ftp服务器通信并将其托管在云中。 To run a schedule task in a hosted server what should I do? 要在托管服务器中运行计划任务,该怎么办? It would be great if someone could point me to a useful online resource. 如果有人可以指出我有用的在线资源,那就太好了。

Thanks in Advance.. 提前致谢..

Most of cloud environments have some kind of scheduler. 大多数云环境都有某种调度程序。 For example Azure . 例如Azure Also list of pretty good scheduling libraries could be found in Scott Hanselmans article here . 此处的 Scott Hanselmans文章中也可以找到相当好的调度库列表。

I used Quartz and it is awesome and straight-forwarded. 我使用了Quartz ,它很棒而且直截了当。

A simple example of usage is something like : 一个简单的用法示例如下:

    using Quartz;

    private ScheduleCopyTask()
    {
        Quartz.IScheduler sched;
        IJobDetail job;
        ITrigger trigger;

        // Instantiate the Quartz.NET scheduler
        var schedulerFactory = new Quartz.Impl.StdSchedulerFactory();
        sched = schedulerFactory.GetScheduler();
        sched.Start();

        // Instantiate the JobDetail object passing in the type of your
        // custom job class. Your class merely needs to implement a simple
        // interface with a single method called "Execute".
        job = JobBuilder.Create<SyncJob>()
                     .WithIdentity("SyncJob", "group1").Build();


        // Trigger the job to run now, and then every 30 mins
        trigger = TriggerBuilder.Create()
         .WithIdentity("SyncTrigger", "group1")
         .StartNow()
         .WithSimpleSchedule(x => x
         .WithIntervalInMinutes(30)
         .RepeatForever()).Build();

        sched.ScheduleJob(job, trigger);
    }

And your job class : 和你的工作类别:

    class SyncJob : IJob
    {      
        public void Execute(IJobExecutionContext context)
        {
           // your task goes here
        }
    }

You can find Quartz documentation here : http://quartz-scheduler.org/generated/2.2.1/html/qs-all/ 您可以在这里找到Quartz文档: http : //quartz-scheduler.org/genic/2.2.1/html/qs-all/

I use http://hangfire.io/ for this, it is powerful and yet very simple to use. 为此,我使用http://hangfire.io/ ,它功能强大,但使用起来非常简单。

You could also use http://www.quartz-scheduler.net/ , but I haven't use it so I can't comment on it. 您也可以使用http://www.quartz-scheduler.net/ ,但是我没有使用它,因此无法对此发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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