How to cancel a job

Hi

I have a method like

public static void Method1(IJobCancellationToken token)
    {

        token.ThrowIfCancellationRequested();
        .
        .
        .
        WriteOnTextFile();
    }

and I made a job for method1 like :

BackgroundJob.Schedule(() => Method1(null),TimeSpan.FromDays(1));

and then I want to cancel the job which is not executed yet. Should I write a code like this :

BackgroundJob.Schedule(() => Method1(new JobCancellationToken(true)),TimeSpan.FromDays(1));

or use

BackgroundJob.Delete 

If the second one, then what’s the usage of

Method1(new JobCancellationToken(true))

with true parameter.

The second one, ThrowIfCancellationRequested method will throw if you delete a job.

var jobId = BackgroundJob.Schedule(
    () => Method1(JobCancellationToken.Null),
    TimeSpan.FromDays(1));

BackgroundJob.Delete(jobId);
1 Like