-1

I have this code:

    public void OnDisappearing()
    {
        runPointsChecker = false;
        timerSeconds = 0;
        cts.Cancel();
        //cts.Token.ThrowIfCancellationRequested();

        if (Settings.Mode == Enums.MO.Quiz && App.showCard.Running())
            StopQuiz();

        //Code to Stop the stop watch and insert the values in the database.
        ScreenTimeStopWatch.StopScreenTimeStopwatchAndToDatabase();
        ScreenTimeStopWatch.KillScreenTimeStopwatch();
    }

The code block is failing with a null object exception. Can someone give me some suggestions on how I could add to this to protect this from happening?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • 4
    `cts?.Cancel();` eventually? But where exactly is it failing? Once the `CancellationTokenSource` is created, it's `Token`property is also created. But where is this `cts` coming from? Why can't you ensure that it is always there? What is your intention with calling `Cancel` and throwing unhandled(?) `OperationCancelledException` right after? – ZorgoZ Jan 01 '20 at 14:54
  • You should pass the token to task or operation, which you want to cancel – Pavel Anikhouski Jan 01 '20 at 15:16

2 Answers2

0

If you mean that cts is null when you try and call Cancel() on it.. a simple null check would suffice!

if (cts != null)
{
    cts.Cancel();
}  
Ilan
  • 513
  • 2
  • 8
  • 24
0

According to your description, I guess that you want to use CancellationTokenSource in your code, I do one sample that you can take a look:

The CancellationTokenSource is used to cancel a Task.

 CancellationTokenSource cts;
    private async void Start_Clicked(object sender, EventArgs e)
    {
        cts = new CancellationTokenSource();
        try
        {
            // ***Send a token to carry the message if cancellation is requested.
            await task1(cts.Token);

        }
        // *** If cancellation is requested, an OperationCanceledException results.
        catch (OperationCanceledException)
        {
            Console.WriteLine("the request is canceled!");
        }

        cts = null;
    }

    private void Cancel_Clicked(object sender, EventArgs e)
    {
        if (cts != null)
        {
            cts.Cancel();
        }
    }
    Task task1(CancellationToken ct)
    {
        TaskCompletionSource<DateTime> tcs = new TaskCompletionSource<DateTime>();
        Task.Run(() =>
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine("the valuie is {0}", i);
                if (ct.IsCancellationRequested == true)
                {
                    Console.WriteLine("request cancel!");
                    tcs.SetCanceled();
                    break;

                }

            }

        }, ct);

        return tcs.Task;

    }

You can change your code according to my example.

halfer
  • 19,824
  • 17
  • 99
  • 186
Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16