✅ [SOLVED] Need help with converting async method to a 'normal' method
I tried a few solutions to be able to call an async method from my Main method but those have failed, here is the link:
https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c
Here is my code:
So how do I go about converting this to something that I can easily call like a normal function from Main?
23 Replies
why not just make ur Main async?
was about to say
that's also kinda the opposite of what the title says
and
are both valid entry points and do more or the same
I wouldn't know why would I go async, I am too used to the old ways of coding something that works, speed is not really a concern here but I would like to hear how would you do that?
So how do I go about converting this to something that I can easily call like a normal function from MainYou don't, you have async work to do, so your method must be async. Also, the method is ALREADY a "normal" function that can easily be called from Main()
async doesnt mean that its faster, its just async ;p
you missed
async
on Main
noticed and fixed ;p thx for pointing out 🙂
thanks for the advise, I will see what I can do after the changes
on the rare occasion that you DO need to call an async method from a non-async method, and wait for its completion, you do
avoid doing this unless you have no other option
Somehow, it actually worked this time. So the last time I was trying to run it, my IDE had automatically added
async
as part of my main method, something like static async Main(string[] args)
Now I rebuilt it and can finally run it
I don't know how to explain this :/
but it worked
thanks 🙂async methods must return
Task
99.99% of the timeby returning task do you mean something like
await Task.Delay(1000);
?no, I mean defining the method's return type to be
Task
or Task<T>
Got it
the Main method can come in a lot of variants
static int Main(string[] args)
static int Main()
static void Main(string[] args)
static void Main()
and the same for async, instead of void
it would be Task
and instead of int
it would be Task<int>
(and it can be either just static
or static async
)uhh, why'd you list each version twice?
oh
fixed 😂
I am getting familiar, thanks for sharing
lets mention top-level statements for completion as well:
can also be a complete "main method" (the compiler automatically builds a Program class with the Main method then)
lets mention top-level statements for completion as well:could we, instead, not? 😆
too late 😒
$close
Use the
/close
command to mark a forum thread as answered