Repeating Task throws null exception after a while

I have a code that runs every minute in an Activity, because i have put KeepScreenOn Flag i though i can run this method without problem:

 private async Task UpdateData(CancellationToken token)
        {
            Vibrator vibrator = (Vibrator)this.ApplicationContext.GetSystemService(Context.VibratorService);
            while (!token.IsCancellationRequested)
            {
                await Task.Delay(60000);
                // Get ETA
                try
                {
                    query = ParseObject.GetQuery("CaseDetails");
                    results = await query.FindAsync();
                    resultsList = results.ToList();
                    foreach (var temp in resultsList)
                    {
                        // If ETA is changed, Update ETA and vibrate
                        if (Convert.ToDateTime(application.caseObject["ETA"]) != temp.Get<DateTime>("ETA"))
                        {
                            ctsETA.Cancel();
                            ctsETA = new CancellationTokenSource();
                            _tokenETA = ctsETA.Token;
                            application.caseObject = temp;
                            dateDiff = Convert.ToDateTime(application.caseObject["ETA"]) - DateTime.Now;
                            StartCountdownTimer(ctsETA.Token, dateDiff);
                            vibrator.Vibrate(500);
                        }
                    }

                }
                catch (Exception ex)
                {
                    Console.Write("Exception occured: " + ex);
                }
            }
        }

But some times i get object null exception on results = await query.FindAsync(); and i cant understand why. Is it because of a problem with the code or because i should use background Service for this?
thanks

Sign In or Register to comment.