© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
13 replies
UnemployedNinja

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:
private readonly string _Value;
public MyClass() { // Constructor
  Task<string> task = GetMyValue(); // Makes an asynchronous Http request
  while (!task.IsCompleted) Thread.Sleep(10);
  _Value = task.Result;
}
private readonly string _Value;
public MyClass() { // Constructor
  Task<string> task = GetMyValue(); // Makes an asynchronous Http request
  while (!task.IsCompleted) Thread.Sleep(10);
  _Value = task.Result;
}
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? 😬
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ can Thread.Sleep affect performance?
C#CC# / help
3y ago
class constructor in c#
C#CC# / help
12mo ago
Parameterless constructor in struct
C#CC# / help
13mo ago