Call async method in constructor?
I used to call async methods (fire and forgot?) in constructors by
Task.Run(async () => await CallAsync());
I heard it's better to use ICommand
to execute it.
ICommand MyCmd => new Command(async () => await CallAsync());
public MyClass()
{
MyCmd.Execute(null);
}
What's the difference between these two methods?
c#
|
show 2 more comments
I used to call async methods (fire and forgot?) in constructors by
Task.Run(async () => await CallAsync());
I heard it's better to use ICommand
to execute it.
ICommand MyCmd => new Command(async () => await CallAsync());
public MyClass()
{
MyCmd.Execute(null);
}
What's the difference between these two methods?
c#
4
You shouldnt perform any processing in constructors in the first place
– FCin
Nov 21 at 5:31
1
HostingEnvironment.QueueBackgroundWorkItem
is recommended mostly, take a look at here blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html
– Just code
Nov 21 at 5:34
@FCin, it says initializing the WinForms controls in the constructor can be much faster in this question, stackoverflow.com/questions/2521322/….
– ca9163d9
Nov 21 at 6:11
This refers to updating controls. I'm talking about processing/retrieving data which I assume you do, because you clearly await something. Also, you haven't tagged your question with what framework you are using. WinForms I assume?
– FCin
Nov 21 at 6:21
5
You're violating the expectation that when a constructor returns, the object is correctly initialized and ready to be used. Consider instead using a factory that performs the async works and passes the results of that work into an appropriately parameterized constructor for this class instead. That factory method can beasync
.
– Damien_The_Unbeliever
Nov 21 at 7:08
|
show 2 more comments
I used to call async methods (fire and forgot?) in constructors by
Task.Run(async () => await CallAsync());
I heard it's better to use ICommand
to execute it.
ICommand MyCmd => new Command(async () => await CallAsync());
public MyClass()
{
MyCmd.Execute(null);
}
What's the difference between these two methods?
c#
I used to call async methods (fire and forgot?) in constructors by
Task.Run(async () => await CallAsync());
I heard it's better to use ICommand
to execute it.
ICommand MyCmd => new Command(async () => await CallAsync());
public MyClass()
{
MyCmd.Execute(null);
}
What's the difference between these two methods?
c#
c#
asked Nov 21 at 5:28
ca9163d9
8,0742389195
8,0742389195
4
You shouldnt perform any processing in constructors in the first place
– FCin
Nov 21 at 5:31
1
HostingEnvironment.QueueBackgroundWorkItem
is recommended mostly, take a look at here blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html
– Just code
Nov 21 at 5:34
@FCin, it says initializing the WinForms controls in the constructor can be much faster in this question, stackoverflow.com/questions/2521322/….
– ca9163d9
Nov 21 at 6:11
This refers to updating controls. I'm talking about processing/retrieving data which I assume you do, because you clearly await something. Also, you haven't tagged your question with what framework you are using. WinForms I assume?
– FCin
Nov 21 at 6:21
5
You're violating the expectation that when a constructor returns, the object is correctly initialized and ready to be used. Consider instead using a factory that performs the async works and passes the results of that work into an appropriately parameterized constructor for this class instead. That factory method can beasync
.
– Damien_The_Unbeliever
Nov 21 at 7:08
|
show 2 more comments
4
You shouldnt perform any processing in constructors in the first place
– FCin
Nov 21 at 5:31
1
HostingEnvironment.QueueBackgroundWorkItem
is recommended mostly, take a look at here blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html
– Just code
Nov 21 at 5:34
@FCin, it says initializing the WinForms controls in the constructor can be much faster in this question, stackoverflow.com/questions/2521322/….
– ca9163d9
Nov 21 at 6:11
This refers to updating controls. I'm talking about processing/retrieving data which I assume you do, because you clearly await something. Also, you haven't tagged your question with what framework you are using. WinForms I assume?
– FCin
Nov 21 at 6:21
5
You're violating the expectation that when a constructor returns, the object is correctly initialized and ready to be used. Consider instead using a factory that performs the async works and passes the results of that work into an appropriately parameterized constructor for this class instead. That factory method can beasync
.
– Damien_The_Unbeliever
Nov 21 at 7:08
4
4
You shouldnt perform any processing in constructors in the first place
– FCin
Nov 21 at 5:31
You shouldnt perform any processing in constructors in the first place
– FCin
Nov 21 at 5:31
1
1
HostingEnvironment.QueueBackgroundWorkItem
is recommended mostly, take a look at here blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html– Just code
Nov 21 at 5:34
HostingEnvironment.QueueBackgroundWorkItem
is recommended mostly, take a look at here blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html– Just code
Nov 21 at 5:34
@FCin, it says initializing the WinForms controls in the constructor can be much faster in this question, stackoverflow.com/questions/2521322/….
– ca9163d9
Nov 21 at 6:11
@FCin, it says initializing the WinForms controls in the constructor can be much faster in this question, stackoverflow.com/questions/2521322/….
– ca9163d9
Nov 21 at 6:11
This refers to updating controls. I'm talking about processing/retrieving data which I assume you do, because you clearly await something. Also, you haven't tagged your question with what framework you are using. WinForms I assume?
– FCin
Nov 21 at 6:21
This refers to updating controls. I'm talking about processing/retrieving data which I assume you do, because you clearly await something. Also, you haven't tagged your question with what framework you are using. WinForms I assume?
– FCin
Nov 21 at 6:21
5
5
You're violating the expectation that when a constructor returns, the object is correctly initialized and ready to be used. Consider instead using a factory that performs the async works and passes the results of that work into an appropriately parameterized constructor for this class instead. That factory method can be
async
.– Damien_The_Unbeliever
Nov 21 at 7:08
You're violating the expectation that when a constructor returns, the object is correctly initialized and ready to be used. Consider instead using a factory that performs the async works and passes the results of that work into an appropriately parameterized constructor for this class instead. That factory method can be
async
.– Damien_The_Unbeliever
Nov 21 at 7:08
|
show 2 more comments
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405751%2fcall-async-method-in-constructor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405751%2fcall-async-method-in-constructor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
You shouldnt perform any processing in constructors in the first place
– FCin
Nov 21 at 5:31
1
HostingEnvironment.QueueBackgroundWorkItem
is recommended mostly, take a look at here blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html– Just code
Nov 21 at 5:34
@FCin, it says initializing the WinForms controls in the constructor can be much faster in this question, stackoverflow.com/questions/2521322/….
– ca9163d9
Nov 21 at 6:11
This refers to updating controls. I'm talking about processing/retrieving data which I assume you do, because you clearly await something. Also, you haven't tagged your question with what framework you are using. WinForms I assume?
– FCin
Nov 21 at 6:21
5
You're violating the expectation that when a constructor returns, the object is correctly initialized and ready to be used. Consider instead using a factory that performs the async works and passes the results of that work into an appropriately parameterized constructor for this class instead. That factory method can be
async
.– Damien_The_Unbeliever
Nov 21 at 7:08