C
C#•15mo ago
bighugemassive3

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
Tvde1•15mo ago
Why don't you want to use await?
bighugemassive3
bighugemassive3OP•15mo ago
Trying to reduce how much I use it because it makes the stack frames really messy
Tvde1
Tvde1•15mo 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;
}
bighugemassive3
bighugemassive3OP•15mo ago
Still uses await though 😦
Tvde1
Tvde1•15mo ago
you can't avoid using await 😬 if you use ContinueWIth you get even worse problems
bighugemassive3
bighugemassive3OP•15mo 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
Tvde1•15mo ago
are you using a modern .NET version? I also like to avoid Exceptions as much as possible
bighugemassive3
bighugemassive3OP•15mo ago
.NET Standard 2.0
JakenVeina
JakenVeina•15mo ago
that was resolved years ago, what is your tooling setup?
bighugemassive3
bighugemassive3OP•15mo ago
What's that?
JakenVeina
JakenVeina•15mo ago
do not try and re-invent awaiting, it's not trivial whatever is giving you "messy" stack traces
bighugemassive3
bighugemassive3OP•15mo ago
You mean IDE? I'm using rider
bighugemassive3
bighugemassive3OP•15mo ago
this is what I mean by messy stack trace though
No description
JakenVeina
JakenVeina•15mo ago
...what would you expect it to look like? That is everything that a stack trace should be
bighugemassive3
bighugemassive3OP•15mo ago
Would prefer it without the purple lines 😛
JakenVeina
JakenVeina•15mo 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
bighugemassive3
bighugemassive3OP•15mo 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
JakenVeina•15mo ago
there is in VS
JakenVeina
JakenVeina•15mo 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...
bighugemassive3
bighugemassive3OP•15mo ago
Visual studio's stack trace window is pretty much unintelligible anyway, just looks like a mush of the same colour text
reflectronic
reflectronic•15mo ago
the fix is to hide the purple lines, not mess up your code
Accord
Accord•15mo 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.
Want results from more Discord servers?
Add your server