Do I need to stop a method that's running in a UI thread?

Viewed 1748

My application looks like this:

App class:

protected override void OnStart()
{
   MainPage = new Japanese.MainPage();
}

MainPage class:

var phrasesPage = new NavigationPage(new PhrasesPage())
{
   Title = "Play",
   Icon = "play.png"
};
Children.Add(phrasesPage);

PhrasesPage class:

protected override void OnAppearing()
{
   base.OnAppearing();
   phrasesFrame = new PhrasesFrame(this);
   phrasesStackLayout.Children.Add(phrasesFrame);
}

protected override void OnDisappearing()
{
   base.OnDisappearing();
   phrasesStackLayout.Children.Remove(phrasesFrame);
}

PhrasesFrame class:

public PhrasesFrame(PhrasesPage phrasesPage)
{
   InitializeComponent();
   Device.BeginInvokeOnMainThread(() => ShowCards().ContinueWith((arg) => { }));
}

public async Task ShowCards()
{
   while (true)
   {
      // information displayed on screen here and screen
      // responds to user clicks
      await Task.Delay(1000);
    }
}

Two questions here.

Firstly my ShowCards method has no return as it loops around until a user clicks on another icon on the bottom of the screen to select another screen. In this case what should I code for the return value. As it is the IDE warns that the method never reaches the end or a return statement. How can I fix this problem.

Second related question. As the ShowCards runs on another thread, should I do something to cancel it when the user clicks on another icon to display another screen.

I hope someone can help give me some advice. Please ask if something is not clear so I can try to make the question more clear. Thanks

2 Answers
Related