async task cancellation
this my code
async Task GetStudentOrStaffPhoto(string type, string studentStaffId)
{
try
{
if (Reachability.IsInternetReachable())
{
// Create Singleton instance of the account factory class
SearchFactory searchFactory = SearchFactory.GetInstance;
// Create appropriate account facade instance
var searchFacade = searchFactory.CreateSearchInstance(Entity.Common.SearchTypeEnum.RegularSearch);
var schlCode = NSUserDefaults.StandardUserDefaults.StringForKey(AppConstants.LastSelSchoolCodeUDKey);
var studentStaffPhotoResponse = await searchFacade.GetStudentOrStaffPhoto(schlCode, type, ids); if (studentStaffPhotoResponse.IsResponseSuccess) { if (studentStaffPhotoResponse.Response != null) { var indexToUpdate = pageIndex; //foreach (var item in studentStaffPhotoResponse.Response) //{ for (int i = 0; i < studentStaffPhotoResponse.Response.Count; i++) { if (studentStaffPhotoResponse.Response[i].Image != null) { byte[] sPhoto = Convert.FromBase64String(studentStaffPhotoResponse.Response[i].Image); if (sPhoto != null) { searchResults[indexToUpdate].Photo = sPhoto; InvokeOnMainThread(() => { var visibleRows = SearchResultTableView.IndexPathsForVisibleRows; var lastVisibleRowIndex = visibleRows[visibleRows.Count() - 1].Row; if (indexToUpdate <= lastVisibleRowIndex && pageIndex == 0) { var cell = (SearchResultCell)SearchResultTableView.CellAt(NSIndexPath.FromRowSection(indexToUpdate, 0)); if (cell != null) { //var staffImageView = (UIImageView)cell.ContentView.ViewWithTag(indexToUpdate + 2000); var studentStaffImageView = cell.ContentView.ViewWithTag(indexToUpdate + 2000) as UIImageView; if (searchResults[indexToUpdate].Photo != null) { studentStaffImageView.Image = new UIImage(NSData.FromArray(searchResults[indexToUpdate].Photo)); } } } }); indexToUpdate += 1; } } } } pageIndex += pageSize; if (pageIndex <= searchResults.Count ) { if (Reachability.IsInternetReachable()) { await GetStudentOrStaffPhoto( type, ids); } else { var alert = UIAlertController.Create(SharedConstants.NoInternetTitle, SharedConstants.NoInternetMessage, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create(AppConstants.OK, UIAlertActionStyle.Cancel, null)); PresentViewController(alert, true, null); } } } else if (studentStaffPhotoResponse.IsUnauthorized) { InvokeOnMainThread(() => { CommonHelper.ShowUnauthorizedAlert(this); }); } else { InvokeOnMainThread(() => { var errMsg = !string.IsNullOrEmpty(studentStaffPhotoResponse.ResponseMessage) ? studentStaffPhotoResponse.ResponseMessage : SharedConstants.ServerErrorMessage; var alert = UIAlertController.Create(SharedConstants.ServerErrorTitle, errMsg, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create(AppConstants.OK, UIAlertActionStyle.Cancel, (obj) => { NavigationController.PopViewController(true); })); PresentViewController(alert, true, null); }); } } else { InvokeOnMainThread(() => { var alert = UIAlertController.Create(NSBundle.MainBundle.GetLocalizedString(AppConstants.NoInternetTitleLKey, ""), NSBundle.MainBundle.GetLocalizedString(AppConstants.NoInternetMsgLKey, ""), UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create(NSBundle.MainBundle.GetLocalizedString(AppConstants.OKLKey, ""), UIAlertActionStyle.Cancel, (obj) => { NavigationController.PopViewController(true); })); PresentViewController(alert, true, null); }); } } catch (Exception ex) { CommonHelper.LogException(ex, this); } }