0

I am creating a long running task and want to return an object from task if cancelled

private void HandleBackgroundTasks(object args)
{
  var handles = args as WaitHandle[];
  CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
  while (WaitHandle.WaitAny(handles) == 0 && !cancellationTokenSource.IsCancellationRequested)
  {
     var task = Task.Factory.StartNew(BackgroundTask, cancellationTokenSource.Token);
     cancellationTokenSource.Token.Register(() => ResetCancellationTaskStatus());
  }
}

private void BackgroundTask()
{
  //long running task
  //task creates an object with task status and id
}

private void ResetCancellationTaskStatus()
{
  //Need to update the task status of object created in BackgroundTask method
}
user3441903
  • 77
  • 10
  • _"want to return an object from task if cancelled"_ -- your title says "while the task is in progress". A cancelled task is not "in progress". So, which do you mean? Either way, _what have you tried_? What _specifically_ do you need help with? – Peter Duniho Apr 25 '21 at 20:37
  • 1
    @user344, why did you add the `aync-await` tag? Your sample code does not use that at all. – H H Apr 25 '21 at 21:11
  • Also, it matters a lot in what kind of app (asp.net or not) and how you interface with the 'foreground'. – H H Apr 25 '21 at 21:12

0 Answers0