Thread.sleep in a constructor
Since you can't await in a constructor, I'm calling an async method and just sleeping the thread for 10ms while the task is incomplete:
Is this ok, or is there a better way to do this?
... or should I just be designing my code better in the first place? 😬
8 Replies
No that's terrible.
Don't do anything expensive in your constructor.
You probably want to use a Lazy<T>
if you need to await data your class requires to be constructed you could use a static factory method
that can be made async
That's what I thought :/
hmm I might go this route, ty
ez
Yea, I did it like this lol
What kind of class is this anyhow?
It's used to create a websocket client. The server I'm connecting to has a service API, which is used to get the websocket server for different regions. And the websocket client requires a URL to be created
I could just contact the service API before, and then create the client, but I'd like to do it all in one
It's for a library I'm creating, so I'd like to minimize the steps required
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View