-
|
My console app does not use async, and I use What is the correct pattern to use for detecting execution errors when calling Do I always wrap in a Do I test for Or do I test for If I use a cancellation token, will it trigger IsFaulted or throw an exception? E.g. a variant of the wrapper being used: public bool Execute(Command command, out BufferedCommandResult result)
{
// TODO: do we need to wrap in try/catch?
// Start execution to get the process id or error
result = null;
CommandTask<BufferedCommandResult> task = command.ExecuteBufferedAsync();
if (task.Task.IsFaulted)
{
Log.Error(
"Error executing {ToolType} : {Parameters}",
GetToolType(),
command.Arguments
);
// TODO: Will IsFaulted always have an exception?
Log.Error("{Error}", task.Task.Exception);
return false;
}
Log.Information(
"Executing {ToolType} : ProcessId: {ProcessId}, {Parameters}",
GetToolType(),
task.ProcessId,
command.Arguments
);
// Wait for the process to complete
result = task.Task.Result;
// TODO: Is IsFaulted always true if the process fails or is cancelled?
return !task.Task.IsFaulted;
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
If you call |
Beta Was this translation helpful? Give feedback.
If you call
GetAwaiter().GetResult()instead of.Result, it should propagate exceptions correctly. But I would recommend just making everything async instead. Console app's main method can be async as well.