0

I have a phone application. When a screen displays I start a timer like this:

 base.OnAppearing();
 {
     timerRunning = true;
     Device.BeginInvokeOnMainThread(() => showGridTime(5));
 }
 async void showGridTime(int time)
 { 
     while (timerRunning)
     {
         var tokenSource = new CancellationTokenSource();
         await Task.Delay(time, tokenSource.Token);
         detailGrid.IsVisible = true;
     }
 }

The code seems to work but there is a warning message in the IDE saying that an async method cannot return null.

Given this code can someone help and give me advice on what I should return and if I am going about this in the correct way?

2

1 Answer 1

4

Just return a task:

async Task ShowGridTimeAsync(int time)
 { 
     while (timerRunning)
     {
         var tokenSource = new CancellationTokenSource();
         await Task.Delay(time, tokenSource.Token);
         detailGrid.IsVisible = true;
     }
 }

This is necessary to have the caller of this method know when it is completed and act accordingly.

It is not recommended to create async void methods unless you're creating an event or forced to do that in order to meet an interface signature.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.