How to load data in Winforms asynchronously using Task
I have a Winforms C# based MDI application and I have 3 Forms
- MainForm which is my parent form
- Child form which is the child of the above mentioned form
- BaseView both the above forms are inherited from this base class
If you see in my BaseView, I am using Task to load the data in the background thread without freezing the UI, which works fine and the app quickly shows up. Now the problem is that while this data loading is happening in the background I am opening my child form but the child form is unable to get the Db connection because the background thread is still working, is there anyway that I can do kind of signalling to my child form that now the background Db connection has been set and now the child form can load the data.
Or shall I change the way I am trying to achieve this.
Please advice.
public partial class BaseView : Form
{
public BaseView()
{
var firstTimeInitializationTask = new System.Threading.Tasks.Task(() => { FirstTimeInitialization(); });
firstTimeInitializationTask.Start();
}
private void FirstTimeInitialization()
{
Thread.Sleep(10000); // just put this for testing purpose
DbConnection.Connect(CommonRoutines.DbConnectionString);
}
}
c# .net winforms async-await
|
show 7 more comments
I have a Winforms C# based MDI application and I have 3 Forms
- MainForm which is my parent form
- Child form which is the child of the above mentioned form
- BaseView both the above forms are inherited from this base class
If you see in my BaseView, I am using Task to load the data in the background thread without freezing the UI, which works fine and the app quickly shows up. Now the problem is that while this data loading is happening in the background I am opening my child form but the child form is unable to get the Db connection because the background thread is still working, is there anyway that I can do kind of signalling to my child form that now the background Db connection has been set and now the child form can load the data.
Or shall I change the way I am trying to achieve this.
Please advice.
public partial class BaseView : Form
{
public BaseView()
{
var firstTimeInitializationTask = new System.Threading.Tasks.Task(() => { FirstTimeInitialization(); });
firstTimeInitializationTask.Start();
}
private void FirstTimeInitialization()
{
Thread.Sleep(10000); // just put this for testing purpose
DbConnection.Connect(CommonRoutines.DbConnectionString);
}
}
c# .net winforms async-await
1
Tasks aren't threads. Don't use Task.Start like that. Apart from that it's impossible to answer because you didn't post anything that can run in the background. In general, ADO.NET has asynchronous methods likeExecuteQueryAsyncetc
– Panagiotis Kanavos
Nov 14 '18 at 11:23
2
DbConnection.Connectis extremely suspicious by the way. Having a global connection is a serious bug. Connections should be opened right before they are used and closed immediatelly when no longer needed.DbConnectionis the name of the abstract base class for all ADO.NET connection classes too. Those classes all haveOpenAsyncmethods to open without blocking
– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
@adam.k that would only lead to race conditions.
– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
@Shax what are you trying to do? What isFirstTimeInitializationsupposed to do and why do that in the constructor? Removing the global connection will solve the immediate problem and a lot of others as well
– Panagiotis Kanavos
Nov 14 '18 at 11:27
1
We would not have known that from what was shown, but either way, that is a poor design. This is turning into an XY problem.
– Nkosi
Nov 14 '18 at 11:45
|
show 7 more comments
I have a Winforms C# based MDI application and I have 3 Forms
- MainForm which is my parent form
- Child form which is the child of the above mentioned form
- BaseView both the above forms are inherited from this base class
If you see in my BaseView, I am using Task to load the data in the background thread without freezing the UI, which works fine and the app quickly shows up. Now the problem is that while this data loading is happening in the background I am opening my child form but the child form is unable to get the Db connection because the background thread is still working, is there anyway that I can do kind of signalling to my child form that now the background Db connection has been set and now the child form can load the data.
Or shall I change the way I am trying to achieve this.
Please advice.
public partial class BaseView : Form
{
public BaseView()
{
var firstTimeInitializationTask = new System.Threading.Tasks.Task(() => { FirstTimeInitialization(); });
firstTimeInitializationTask.Start();
}
private void FirstTimeInitialization()
{
Thread.Sleep(10000); // just put this for testing purpose
DbConnection.Connect(CommonRoutines.DbConnectionString);
}
}
c# .net winforms async-await
I have a Winforms C# based MDI application and I have 3 Forms
- MainForm which is my parent form
- Child form which is the child of the above mentioned form
- BaseView both the above forms are inherited from this base class
If you see in my BaseView, I am using Task to load the data in the background thread without freezing the UI, which works fine and the app quickly shows up. Now the problem is that while this data loading is happening in the background I am opening my child form but the child form is unable to get the Db connection because the background thread is still working, is there anyway that I can do kind of signalling to my child form that now the background Db connection has been set and now the child form can load the data.
Or shall I change the way I am trying to achieve this.
Please advice.
public partial class BaseView : Form
{
public BaseView()
{
var firstTimeInitializationTask = new System.Threading.Tasks.Task(() => { FirstTimeInitialization(); });
firstTimeInitializationTask.Start();
}
private void FirstTimeInitialization()
{
Thread.Sleep(10000); // just put this for testing purpose
DbConnection.Connect(CommonRoutines.DbConnectionString);
}
}
c# .net winforms async-await
c# .net winforms async-await
asked Nov 14 '18 at 11:14
ShaxShax
92082853
92082853
1
Tasks aren't threads. Don't use Task.Start like that. Apart from that it's impossible to answer because you didn't post anything that can run in the background. In general, ADO.NET has asynchronous methods likeExecuteQueryAsyncetc
– Panagiotis Kanavos
Nov 14 '18 at 11:23
2
DbConnection.Connectis extremely suspicious by the way. Having a global connection is a serious bug. Connections should be opened right before they are used and closed immediatelly when no longer needed.DbConnectionis the name of the abstract base class for all ADO.NET connection classes too. Those classes all haveOpenAsyncmethods to open without blocking
– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
@adam.k that would only lead to race conditions.
– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
@Shax what are you trying to do? What isFirstTimeInitializationsupposed to do and why do that in the constructor? Removing the global connection will solve the immediate problem and a lot of others as well
– Panagiotis Kanavos
Nov 14 '18 at 11:27
1
We would not have known that from what was shown, but either way, that is a poor design. This is turning into an XY problem.
– Nkosi
Nov 14 '18 at 11:45
|
show 7 more comments
1
Tasks aren't threads. Don't use Task.Start like that. Apart from that it's impossible to answer because you didn't post anything that can run in the background. In general, ADO.NET has asynchronous methods likeExecuteQueryAsyncetc
– Panagiotis Kanavos
Nov 14 '18 at 11:23
2
DbConnection.Connectis extremely suspicious by the way. Having a global connection is a serious bug. Connections should be opened right before they are used and closed immediatelly when no longer needed.DbConnectionis the name of the abstract base class for all ADO.NET connection classes too. Those classes all haveOpenAsyncmethods to open without blocking
– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
@adam.k that would only lead to race conditions.
– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
@Shax what are you trying to do? What isFirstTimeInitializationsupposed to do and why do that in the constructor? Removing the global connection will solve the immediate problem and a lot of others as well
– Panagiotis Kanavos
Nov 14 '18 at 11:27
1
We would not have known that from what was shown, but either way, that is a poor design. This is turning into an XY problem.
– Nkosi
Nov 14 '18 at 11:45
1
1
Tasks aren't threads. Don't use Task.Start like that. Apart from that it's impossible to answer because you didn't post anything that can run in the background. In general, ADO.NET has asynchronous methods like
ExecuteQueryAsync etc– Panagiotis Kanavos
Nov 14 '18 at 11:23
Tasks aren't threads. Don't use Task.Start like that. Apart from that it's impossible to answer because you didn't post anything that can run in the background. In general, ADO.NET has asynchronous methods like
ExecuteQueryAsync etc– Panagiotis Kanavos
Nov 14 '18 at 11:23
2
2
DbConnection.Connect is extremely suspicious by the way. Having a global connection is a serious bug. Connections should be opened right before they are used and closed immediatelly when no longer needed. DbConnection is the name of the abstract base class for all ADO.NET connection classes too. Those classes all have OpenAsync methods to open without blocking– Panagiotis Kanavos
Nov 14 '18 at 11:24
DbConnection.Connect is extremely suspicious by the way. Having a global connection is a serious bug. Connections should be opened right before they are used and closed immediatelly when no longer needed. DbConnection is the name of the abstract base class for all ADO.NET connection classes too. Those classes all have OpenAsync methods to open without blocking– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
1
@adam.k that would only lead to race conditions.
– Panagiotis Kanavos
Nov 14 '18 at 11:24
@adam.k that would only lead to race conditions.
– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
1
@Shax what are you trying to do? What is
FirstTimeInitialization supposed to do and why do that in the constructor? Removing the global connection will solve the immediate problem and a lot of others as well– Panagiotis Kanavos
Nov 14 '18 at 11:27
@Shax what are you trying to do? What is
FirstTimeInitialization supposed to do and why do that in the constructor? Removing the global connection will solve the immediate problem and a lot of others as well– Panagiotis Kanavos
Nov 14 '18 at 11:27
1
1
We would not have known that from what was shown, but either way, that is a poor design. This is turning into an XY problem.
– Nkosi
Nov 14 '18 at 11:45
We would not have known that from what was shown, but either way, that is a poor design. This is turning into an XY problem.
– Nkosi
Nov 14 '18 at 11:45
|
show 7 more comments
0
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%2f53298916%2fhow-to-load-data-in-winforms-asynchronously-using-task%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
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.
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%2f53298916%2fhow-to-load-data-in-winforms-asynchronously-using-task%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
1
Tasks aren't threads. Don't use Task.Start like that. Apart from that it's impossible to answer because you didn't post anything that can run in the background. In general, ADO.NET has asynchronous methods like
ExecuteQueryAsyncetc– Panagiotis Kanavos
Nov 14 '18 at 11:23
2
DbConnection.Connectis extremely suspicious by the way. Having a global connection is a serious bug. Connections should be opened right before they are used and closed immediatelly when no longer needed.DbConnectionis the name of the abstract base class for all ADO.NET connection classes too. Those classes all haveOpenAsyncmethods to open without blocking– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
@adam.k that would only lead to race conditions.
– Panagiotis Kanavos
Nov 14 '18 at 11:24
1
@Shax what are you trying to do? What is
FirstTimeInitializationsupposed to do and why do that in the constructor? Removing the global connection will solve the immediate problem and a lot of others as well– Panagiotis Kanavos
Nov 14 '18 at 11:27
1
We would not have known that from what was shown, but either way, that is a poor design. This is turning into an XY problem.
– Nkosi
Nov 14 '18 at 11:45