Deny task. Continue with exception

I am trying to prevent the task from completing if the first part does not work.

My code is as follows:

Task listener = Task.Factory.StartNew(openConnection).ContinueWith((t) => listenForNumber());

    void openConnection()
    {
        try
        {
           //stuff
        }
        catch
        {
          //morestuff
        }
    }

    void listenForNumber()
    {
       //even more stuff
    }

Now listenForNuber () should not be executed if openConnection () is included in the catch block

I tried ContinueWith((t) => listenForNumber(),TaskContinuationOptions.NotOnFaulted);

But no success, no help ?: (

thank

+5
source share
3 answers

TaskContiuationOptions.NotOnFaultedobviously will have no effect if your method did not work, i.e. the exception that occurred during its execution was unhandled.

catch ( ) throw; (, ) - , "".

+8

.

public static void PropagateExceptions(this Task task)
{
    if (task == null)
        throw new ArgumentNullException("task");
    if (!task.IsCompleted)
        throw new InvalidOperationException("The task has not completed yet.");

    if (task.IsFaulted)
        task.Wait();
}

PropagateExceptions() . PropagateExceptions() , .

t1.ContinueWith(t => { 
    t.PropagateExceptions();
    listenForNumber(); 
});
+2

. TPL , , .

- . , .

, , .

+1
source

All Articles