Trying to return a bool from async function but failed #237
-
Hi, I am using CliWrap to run a series of exe. private static async Task<BufferedCommandResult> RunStep1( ) {
var result1 = await Cli.Wrap(executable1Path)
.WithWorkingDirectory(cwd)
.WithArguments(args => args
.Add("arg1")
.Add("opt1")
.Add("arg2")
.Add("opt2")
).ExecuteBufferedAsync(cts.Token);
}
private static async Task<BufferedCommandResult> RunStep2( ) {
var result1 = await Cli.Wrap(executable2Path)
.WithWorkingDirectory(cwd)
.WithArguments(args => args
.Add("arg1")
.Add("opt1")
.Add("arg2")
.Add("opt2")
).ExecuteBufferedAsync(cts.Token);
......
} Then I combine these single steps into one: public static async void RunPipeline( ) {
try
{
result = await RunStep1();
}
catch (OperationCanceledException ex)
{
#if DEBUG
MessageBox.Show("Step 1 error: " + ex.Message.ToString());
#endif
throw ex;
}
try
{
result = await RunStep2();
}
catch (OperationCanceledException ex)
{
#if DEBUG
MessageBox.Show("Step 2 error: " + ex.Message.ToString());
#endif
throw ex;
}
......
} and this Then, I want to return a bool from the public static async Task<bool> RunPipelineBool( ) {
try
{
result = await RunStep1();
}
catch (OperationCanceledException ex)
{
#if DEBUG
MessageBox.Show("Step 1 error: " + ex.Message.ToString());
#endif
throw ex;
}
try
{
result = await RunStep2();
}
catch (OperationCanceledException ex)
{
#if DEBUG
MessageBox.Show("Step 2 error: " + ex.Message.ToString());
#endif
throw ex;
}
if(some condition mathes) {
return false;
} else {
return true;
}
......
} and I tried to get the result as follows: Task<bool> task = RunPipelineBool();
bool isSuccess = task.Result;
if (isSuccess ) {
......
} else {
......
} However, when I run the above code, the program stucks , but it does not report any error (by setting the breakpoint I can see that the program starts running the first exe, but then it stucks and does not report any error). I am wondering if I made some mistake in getting the bool. I appreciate if anyone could point it out. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You should do https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/async-scenarios |
Beta Was this translation helpful? Give feedback.
You should do
var isSuccess = await RunPipelineBool()
.https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/async-scenarios