Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz.NET error comes every time I use it

I get an error in this line saying:

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'Quartz.IScheduler'. An explicit conversion exists (are you missing a cast?)

How to solve it; I don't understand? Please help!

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

   public static void Start()
    {
        try
        {
            //Construct scheduler factory
            //IScheduler scheduler = schedulerFactory.GetScheduler();

           // IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();

            IJobDetail job = JobBuilder.Create<HelloJob>()
                .WithIdentity("jobName", "jobGroup")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(s => s.WithIntervalInSeconds(60).RepeatForever())
                .StartNow()
                .Build();

            scheduler.ScheduleJob(job, trigger);

           // scheduler.Start();

        }

        catch (SchedulerException se)
        {
            //Console.WriteLine(se);
        }
    }
}

public class HelloJob : IJob
{
    private TBPESContext db = new TBPESContext();
    public void Execute(IJobExecutionContext context)
    {
        var AuthorName = from twitterAccount in db.Twitter_Account
                         from c in twitterAccount.Courses
                         select twitterAccount.Author_Name;

        foreach (var item in AuthorName)
        {
            TweetCrawler.SaveTweets(item);
        }


        throw new NotImplementedException();
    }
}
like image 926
user5231960 Avatar asked Nov 29 '22 22:11

user5231960


2 Answers

From version 3.0.0 Quartz: https://www.quartz-scheduler.net/2017/12/30/quartznet-3.0-released.html :

  • SimpleThreadPool is gone, old owned threads are gone

  • IJob interface now returns a task

So I put here the example to use:

class Program
{
    static void Main(string[] args)
    {
        JobScheduler jobScheduler = new JobScheduler();
        jobScheduler.Start();
        Console.ReadLine();
    }
}
public class JobScheduler
{
     public async void Start()
    {
        ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
        IScheduler scheduler = await schedulerFactory.GetScheduler();
        await scheduler.Start();

        IJobDetail job = JobBuilder.Create<HelloJob>().Build();

        ITrigger trigger = TriggerBuilder.Create()

            .WithIdentity("HelloJob ", "GreetingGroup")

            .WithCronSchedule("0 0/1 * 1/1 * ? *")

            .StartAt(DateTime.UtcNow)

            .WithPriority(1)

            .Build();

        await scheduler.ScheduleJob(job, trigger);

    }

}
public class HelloJob : IJob
{
    Task IJob.Execute(IJobExecutionContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        Task taskA = new Task(() => Console.WriteLine("Hello from task at {0}", DateTime.Now.ToString()));
        taskA.Start();
        return taskA;
    }
}
like image 63
Cycloguy Avatar answered Dec 18 '22 21:12

Cycloguy


I can't explain perfectly, but I know how it is working.

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

You need to get the result of the GetDefaultScheduler()so it looks like:

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
like image 26
Noel Nemeth Avatar answered Dec 18 '22 22:12

Noel Nemeth