internal static void Run(CancellationTokenSource cts) { try { var token = cts.Token; var longTask = new LongRunningTask(); t = Task.Factory.StartNew(() => { longTask.Process(token); }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default); t.Wait(); } catch (AggregateException ae) { ae.Flatten().Handle(ex => { if (ex is Exception) { Task.Run(() => { Console.WriteLine(ex.Message); }); throw ex; } else { return(false); } }); } }
public JsonResult Run(CancellationTokenSource cancellationTokenSource) { try { _cancellationTokenSource = cancellationTokenSource; var longTask = new LongRunningTask(); _longTask = Task.Factory.StartNew(() => { longTask.Process(_cancellationTokenSource.Token); }, _cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); _longTask.Wait(); } catch (AggregateException ae) { ae.Flatten().Handle(ex => { if (ex is Exception) { Task.Run(() => { return(Json("Specific OperationCanceledException", JsonRequestBehavior.AllowGet)); }); throw ex; } else { return(false); } }); } return(Json("The Operation Started successfully .", JsonRequestBehavior.AllowGet)); }