i have some code in an object constructor similar to
delegate DataSet MyInvoker;
public MyObject(Param1 p1)
{
// property sets here
// ...
BeginMyAsyncMethod();
}
public void BeginMyAsyncMethod()
{
// set some properties
// ...
MyInvoker inv = new MyInvoker(SomeBeginMethod);
inv.BeginInvoke(new AsyncCallback(SomeEndMethod), null);
}
My questions are:
This answer gives me the impression that leaving it to the user is bad practice although I am talking specifically about starting async methods in the constructor, not about the correct construction of an object.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.
Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
This is easily accomplished with a slightly different approach. In all reality, this happens all the time, doesn't it? Here's a simple solution to give you an option without doing something dumb:
public class MyResource
{
// do this instead of a constructor
public async Task<MyResource> StartAsync()
{
await Task.Delay(1);
return this;
}
}
public class MyControl
{
public MyResource Resource { get; set; }
async void Button_Click(object s, EventArgs e)
{
// call start as if it is a constructor
this.Resource = await new MyResource().StartAsync();
}
}
It's typically never a good idea to do something in your constructor that is not directly related to creating the object instance. For example, if I didn't know the purpose of your MyObject
class, I might not know that it spawns an asynchronous process when I create a new instance. That's generally a bad practice.
It almost sounds like you're saying; hey, create this object, and it's only going to be usable when this async process is finished; that's counter-intuitive.
If you want to do something like this, I would think you would be definitely served better by going to a factory pattern; you call a factory like so and it will create the object and call the method for you:
var instance = MyObjectBuilder.CreateInstance();
// Internally, this does
// var x = new MyObject();
// x.Initilizatize();
// return x;
If your object wont be usable until it's been finished initializing, then you should probably expose a property to check if it is ready, like so:
instance.WaitForReady(); // blocking version
if(instance.IsReady) // non blocking check
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With