PrepAway - Latest Free Exam Questions & Answers

You need to ensure that the second operation is invoked only if the data processing operation throws an unhand

You use the Task.Run() method to launch a long-running data processing operation. The data
processing operation often fails in times of heavy network congestion.
If the data processing operation fails, a second operation must clean up any results of the first
operation.
You need to ensure that the second operation is invoked only if the data processing operation
throws an unhandled exception.
What should you do?

PrepAway - Latest Free Exam Questions & Answers

A.
Create a TaskCompletionSource<T> object and call the TrySetException() method of the object.

B.
Create a task by calling the Task.ContinueWith() method.

C.
Examine the Task.Status property immediately after the call to the Task.Run() method.

D.
Create a task inside the existing Task.Run() method by using the AttachedToParent option.

7 Comments on “You need to ensure that the second operation is invoked only if the data processing operation throws an unhand

  1. playhard says:

    You can use the TaskContinuationOptions.AttachedToParent option so that the parent and child tasks are synchronized. this would also work.
    Option B doesnt exist
    never heard of A




    0



    0
  2. Lord Vader says:

    Daniel says:
    November 20, 2016 at 10:30 am
    Answer id B

    static void Main(string[] args)
    {
    var task = Task.Run(() =>
    {
    throw new Exception();
    }).ContinueWith(ContinuationAction);

    task.Wait();

    Console.ReadKey();
    }

    private static void ContinuationAction(Task task)
    {
    if (task.IsFaulted)
    {
    return;
    }

    Console.WriteLine(“Done.”);
    }




    0



    0
  3. ccc says:

    Correct answer is D. We must create child task, attached to parent and TaskContinuationOption OnlyOnFaulted. Then out second task will be invoked only when parent task throws an unhandled exception.




    0



    0
  4. Mario says:

    I dont get it… What about the “second operation is invoked ONLY IF the data processing operation throws an unhandled exception”, While Task.ContinueWith() continues even if it does not throws exceptions.




    1



    0

Leave a Reply