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();
}
}
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:
I ran into the same issue. When I loaded quartz from Manage NuGet Packages it gave me Version: 3.0.0-alpha2 (Prerelease). This caused the error that you are currently seeing. I found an earlier version at https://www.nuget.org/packages/Quartz/2.3.3 followed the instructions to install it, rebuilt my program and it worked like it should.
If you're in a
async/await
context, use @Cycloguy's answer. Else (e.g. registeringIScheduler
in a DI-Container):I can't explain perfectly, but I know how it is working.
You need to get the result of the
GetDefaultScheduler()
so it looks like: