C
C#•3mo ago
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? 😬
8 Replies
mtreit
mtreit•3mo ago
No that's terrible. Don't do anything expensive in your constructor. You probably want to use a Lazy<T>
Jimmacle
Jimmacle•3mo ago
if you need to await data your class requires to be constructed you could use a static factory method that can be made async
UnemployedNinja
UnemployedNinja•3mo ago
That's what I thought :/ hmm I might go this route, ty
Angius
Angius•3mo ago
public class Foo
{
private Foo(X x){}
public static async Task<Foo> Instantiate()
{
var x = await GetX();
return new Foo(x);
}
}
public class Foo
{
private Foo(X x){}
public static async Task<Foo> Instantiate()
{
var x = await GetX();
return new Foo(x);
}
}
ez
UnemployedNinja
UnemployedNinja•3mo ago
Yea, I did it like this lol
jcotton42
jcotton42•3mo ago
What kind of class is this anyhow?
UnemployedNinja
UnemployedNinja•3mo ago
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
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts
Ebay's new "RESTFUL" api's seem to have done away with seller JSON responsesIt looks like I can't just query my own ebay listings (auctions, buynows). They have some weird scheNpgsql.NpgsqlOperationInProgressException: A Command is Already in Progress in MassTransitHey peeps, I've been struggling with this issue for a few days now, I'm trying to publish outbox mesThe best-practice way to use a plausibility checkSo, I have the following class ```csharp namespace CleanLib.Lego.Parts; public class Color : EntitMaking a field eligible for GCrecently i've been working on a small game engine and i'm trying to call the GC to free my entities Object reference not set to an instance of an objectI'm making a simple UDP packet sender but for some reason I have this error when I attempt to start how to use 'like' in SSRSHI ALL. i have a query where i'm trying to use 'like' with a parameter and it doens't seem to be wor✅ Which is the more correct way of class instantiation?```cs // app.axaml.cs public override void OnFrameworkInitializationCompleted() { if (Applicatio✅ project can't find file that is in bin folder avaloniaHelpers.cs: https://pastebin.com/WSCLp8he TermsOfServiceView.axaml: https://pastebin.com/xEgKRneg Te✅ How should I name my ef core migrations?I'm wondering if there is a good way to name your migrations. I have noticed that, you quickly end-usuggest .net redis libraryI get every minute >150 data items about road situation, item has nested structure. I want to use ca