Wie kann ich die Verarbeitung von DataFlow-Blöcken stoppen, wenn einer der Blöcke die Entscheidung getroffen hat, dass ein Fehler aufgetreten ist, um zu verhindern, dass die nächsten Blöcke ausgeführt werden. Ich dachte, ein Block kann eine Ausnahme auslösen, aber nicht sicher, was der richtige Weg ist, um die weitere Verarbeitungspipeline zu stoppen.Wie kann die Pipeline bei fehlerhaften Blöcken gestoppt werden?
UPDATE:
private async void buttonDataFlow_Click(object sender, EventArgs e)
{
var cells = objectListView.CheckedObjects.Cast<Cell>().ToList();
if (cells == null)
return;
var blockPrepare = new TransformBlock<Cell, Cell>(new Func<Cell, Task<Cell>>(Prepare),
new ExecutionDataflowBlockOptions
{
BoundedCapacity = 10000,
MaxDegreeOfParallelism = Environment.ProcessorCount,
});
var blockPreparationFeedback = new TransformBlock<Cell, Cell>(new Func<Cell, Task<Cell>>(PreparationFeedback),
new ExecutionDataflowBlockOptions
{
BoundedCapacity = 10000,
MaxDegreeOfParallelism = Environment.ProcessorCount,
});
var blockTestMover = new ActionBlock<Cell>(new Func<Cell, Task>(TestMover),
new ExecutionDataflowBlockOptions
{
BoundedCapacity = 10000,
MaxDegreeOfParallelism = Environment.ProcessorCount,
});
blockPrepare.LinkTo(blockPreparationFeedback, new DataflowLinkOptions { PropagateCompletion = true });
blockPreparationFeedback.LinkTo(blockTestMover, new DataflowLinkOptions { PropagateCompletion = true });
foreach (Cell c in cells)
{
var progressHandler = new Progress<string>(value =>
{
c.Status = value;
});
c.Progress = progressHandler as IProgress<string>;
blockPrepare.Post(c);
};
blockPrepare.Complete();
try
{
await blockTestMover.Completion;
}
catch(Exception ee)
{
Console.WriteLine(ee.Message);
}
Console.WriteLine("Done");
}
UPDATE 2:
public ITargetBlock<TInput> CreateExceptionCatchingActionBlock<TInput>(
Func<TInput, Task> action,
Action<Exception> exceptionHandler,
ExecutionDataflowBlockOptions dataflowBlockOptions)
{
return new ActionBlock<TInput>(async input =>
{
try
{
await action(input);
}
catch (Exception ex)
{
exceptionHandler(ex);
}
}, dataflowBlockOptions);
}
Das war genau das, wonach ich suchte! – Pablo
nur eine letzte Sache .. Obwohl ActionBlock der letzte Block ist, habe ich versucht, eine ähnliche Methode zu erstellen, dafür zu machen. Mein Beitrag wurde mit dem Versuch aktualisiert, eine solche Methode zu erstellen. Aber Compiler beklagt sich, dass "Leere" nicht abwarten kann. Ich kann ihn verstehen, aber nicht sicher, wie ich es lösen soll. – Pablo
Die Aktion, die ausgeführt wird, gibt 'Task' zurück. – Pablo