C
C#10mo ago
br4kejet

Can you await a Task and return Task<bool> (with a constant bool value) without the `await` keyword?

I came up with this code which does what I want:
Task myTask = ...
return myTask.ContinueWith(t => true);
Task myTask = ...
return myTask.ContinueWith(t => true);
but the problem is that the t => true part ends up being executed on a worker thread, even if myTask is completed (e.g. status is RanToCompletion). Is there a proper way to do this without checking if myTask is completed?
23 Replies
Tvde1
Tvde110mo ago
Why don't you want to use await?
br4kejet
br4kejet10mo ago
Trying to reduce how much I use it because it makes the stack frames really messy
Tvde1
Tvde110mo ago
is that still a thing nowadays?
Task<bool> MethodWithoutAwait()
{
Task sometask = DoStuff();
return WrapInBool(someTask);
}

async Task<bool> WrapInBool(Task t)
{
await t;
return true;
}
Task<bool> MethodWithoutAwait()
{
Task sometask = DoStuff();
return WrapInBool(someTask);
}

async Task<bool> WrapInBool(Task t)
{
await t;
return true;
}
br4kejet
br4kejet10mo ago
Still uses await though 😦
Tvde1
Tvde110mo ago
you can't avoid using await 😬 if you use ContinueWIth you get even worse problems
br4kejet
br4kejet10mo ago
I guess I could just do return myTask.IsCompleted ? Task.FromResult(true) : myTask.ContinueWith(t => true)... still kinda messy though but at least it works
Tvde1
Tvde110mo ago
are you using a modern .NET version? I also like to avoid Exceptions as much as possible
br4kejet
br4kejet10mo ago
.NET Standard 2.0
JakenVeina
JakenVeina10mo ago
that was resolved years ago, what is your tooling setup?
br4kejet
br4kejet10mo ago
What's that?
JakenVeina
JakenVeina10mo ago
do not try and re-invent awaiting, it's not trivial whatever is giving you "messy" stack traces
br4kejet
br4kejet10mo ago
You mean IDE? I'm using rider
br4kejet
br4kejet10mo ago
this is what I mean by messy stack trace though
No description
JakenVeina
JakenVeina10mo ago
...what would you expect it to look like? That is everything that a stack trace should be
br4kejet
br4kejet10mo ago
Would prefer it without the purple lines 😛
JakenVeina
JakenVeina10mo ago
it shows the logical chain of calls that brought you to where you are now because those things aren't your code? that's a UI issue, there's a setting for that
br4kejet
br4kejet10mo ago
Well more that it's just filler stuff that doesn't tell me much apart from that it's an async method I wish there was 😦 I think i'm just gonna switch back to async, the code just looks better apart from the stack trace
JakenVeina
JakenVeina10mo ago
there is in VS
JakenVeina
JakenVeina10mo ago
Rider Support | JetBrains
Rider Debugger shows external call stack frames as just "[External ...
At some point in the last year, the new "[External code: n frames]" format started appearing in this call stack window in the debugger. IIRC, before this, you could at least see the names of the me...
br4kejet
br4kejet10mo ago
Visual studio's stack trace window is pretty much unintelligible anyway, just looks like a mush of the same colour text
reflectronic
reflectronic10mo ago
the fix is to hide the purple lines, not mess up your code
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.