1

I am creating a windows 10 app. In a onNavigatedTo I am initializing the content where a bunch of async calls are happening in parallel and when I navigate from the page they continue to run. I want to stop all the async calls before I Navigate from the page as I have common resource and these threads are writing into the common resource even after I navigate from the source page. Is there anyway to do this? Sample code:

foreach(StorageFile sf in list)
{
      StorageItemThumbnail thumb = await sf.GetThumbnailAsync(ThumbnailMode.ListView, thumbnailResize, ThumbnailOptions.ResizeThumbnail);
     //Process the thumbnail and put it in common resouce
}
AbsoluteSith
  • 1,917
  • 25
  • 60

1 Answers1

2

You can use the OnNavigatingFrom Event. https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.page.onnavigatingfrom.aspx

This event fires right before your page is left. Then you can cancle your tasks using the CancelationToken. See Cancellation Token in await method for details how to cancle.

Community
  • 1
  • 1
Daniel Meixner
  • 1,829
  • 11
  • 10