Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

c# - Task not in a faulted state when an exception is thrown

I can't seem to figure out why my task is not in a faulted state when an exception is thrown in the following sample (assume engine.Send(...) throws an exception):

       var receiverTask = new Task<Task>(async () => {
            result = await Task.FromResult(engine.Send(someObject));
        });
        receiverTask.Start();
        receiverTask.Wait();

if I do receiverTask.IsFaulted after the receiverTask.Wait() it returns false instead of true as I would expect.

I have been reading that I need to use something like the following construct:

receiverTask.ContinueWith(t => { /* error handling */ },
    TaskContinuationOptions.OnlyOnFaulted);

But I can't seem to figure out how to integrate this into the sample code. Do I call this before the Start()? before the Wait()?

I would like the calling program to somehow be aware that the receiverTask has thrown an exception and failed. Any pointers?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I correctly understand this context, you could leave out the .Start() and use .Wait() somewhat differently.

(I don't know what your engine.Send(someObject) method is, so I have to assume.)

Let's say your method returns a bool (it doesn't matter, but just to write something down.

If it is synchronous, the returned Task could be:

 Task<bool> receiverTask = Task<bool>.Run(async () =>
            await Task<bool>.FromResult(engine.Send(someObject)));

or

 Task<bool> receiverTask = Task.Factory.StartNew(async delegate
 {
    bool _result = await Task<bool>.FromResult(engine.Send(someObject));
    return _result;
 }, TaskScheduler.Default).Unwrap();

If it is asynchronous, the returned Task could be:

 Task<bool> receiverTask = Task.Run(async () =>
            await engine.Send(someObject));

or

 Task<bool> receiverTask = Task.Factory.StartNew(async delegate
 {
    bool _result = await engine.Send(someObject);
    return _result;
 }, TaskScheduler.Current).Unwrap<bool>();


(However, all these are blocking if there's no other async/await declaration).

Once your Task is scheduled, some Continuation condition can be defined to evaluate its status.
If the Task is in some faulty status (canceled, aborted etc.) its .IsCompleted property reports true, but the .Status property is not TaskStatus.RanToCompletion.

 receiverTask.ContinueWith(t =>
 {
    //Continue on faulted
    Console.WriteLine(receiverTask.GetAwaiter().IsCompleted);
    if (receiverTask.IsFaulted)
       Console.WriteLine(receiverTask.Exception.InnerExceptions[0].Message);
 }, TaskContinuationOptions.OnlyOnFaulted).Wait(0);

 receiverTask.ContinueWith(t =>
 {
    //Continue on canceled
    Console.WriteLine(receiverTask.GetAwaiter().IsCompleted);
    if (receiverTask.IsCanceled)
       Console.WriteLine(receiverTask.Exception.InnerExceptions[0].Message);
 }, TaskContinuationOptions.OnlyOnCanceled).Wait(0);

 receiverTask.ContinueWith(t =>
 {
    //Standard behaviour
    Console.WriteLine(receiverTask.GetAwaiter().IsCompleted);
    Console.WriteLine(receiverTask.Status.ToString());
 }, TaskContinuationOptions.None).Wait();

 //This writes only if no errors have been raised
 if (receiverTask.Status == TaskStatus.RanToCompletion)
    Console.WriteLine("Completed: {0}  Result: {1}", receiverTask.GetAwaiter().IsCompleted, receiverTask.Result);


But you might also use a Try/Catch block:

 try
 {
    receiverTask.Wait();

    if (receiverTask.Status == TaskStatus.RanToCompletion)
       Console.WriteLine("Completed: {0}  Result: {1}", receiverTask.GetAwaiter().IsCompleted, receiverTask.Result);
 }
 catch (Exception)
 {
    receiverTask.ContinueWith(t =>
    {
       //With continuation
       if (receiverTask.IsFaulted)
          Console.WriteLine(receiverTask.Exception.InnerExceptions[0].Message);
    }, TaskContinuationOptions.OnlyOnFaulted);

       //or without
       //if (receiverTask.IsCanceled)
       //Console.WriteLine(receiverTask.Exception.InnerExceptions[0].Message);
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...