//We start multiple actions in parallel to delete tweets void EraseTweetsAction(TwitterContext ctx, CancellationToken cancellationToken) { int nextTweetID = getNextTweetIDSync(); #if DEBUG_TEST Random rnd = new Random(); #endif //Are we done? while (nextTweetID != Int32.MinValue) { //We can't cancel here, we have already fetched a new ID and if we cancel here it will never be deteled Tweet tweet = mTweetsCollection[nextTweetID]; //Clear Tweets logic here try { #if DEBUG_TEST Thread.Sleep(sleepFakeWaitMilliseconds); if (rnd.Next() % 3 == 0) // Simulate error { throw new ArgumentNullException(); } else { Exception e = new Exception("Sorry, that page does not exist"); throw new Exception("", e); } #else ulong tid = ulong.Parse(tweet.ID); Status ret = null; DirectMessage ret2 = null; switch (TweetsEraseType) { case ApplicationSettings.EraseTypes.TweetsAndRetweets: ret = ctx.DeleteTweetAsync(tid).Result; break; case ApplicationSettings.EraseTypes.Favorites: ret = ctx.DestroyFavoriteAsync(tid).Result; break; case ApplicationSettings.EraseTypes.DirectMessages: ret2 = ctx.DestroyDirectMessageAsync(tid, true).Result; break; default: break; } #endif tweet.Status = STATUS_DELETED; } catch (Exception ex) { TwitterQueryException exception = ex.InnerException as TwitterQueryException; if (exception != null && exception.StatusCode == System.Net.HttpStatusCode.NotFound) { tweet.Status = STATUS_NOT_FOUND; } else if (exception != null && (exception.StatusCode == System.Net.HttpStatusCode.Unauthorized || exception.StatusCode == System.Net.HttpStatusCode.Forbidden)) { tweet.Status = STATUS_NOT_ALLOWED; } else { tweet.Status = STATUS_ERROR; var tmp = new JsonTweet() { created_at = Helpers.DateTimeToString(tweet.Date), id_str = tweet.ID, text = tweet.Text }; lock (_lockerNotDeletedTweetsLst) { notDeletedTweets.Add(tmp); } } } finally { onDeletedTweetUIUpdate(tweet); } //We cancel once a tweet is completely handeled, we make sure not to request for a new one if (cancellationToken.IsCancellationRequested) { return; } nextTweetID = getNextTweetIDSync(); } }