How to load data in Winforms asynchronously using Task












0















I have a Winforms C# based MDI application and I have 3 Forms




  1. MainForm which is my parent form

  2. Child form which is the child of the above mentioned form

  3. 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);
}
}









share|improve this question


















  • 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






  • 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






  • 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 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





    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
















0















I have a Winforms C# based MDI application and I have 3 Forms




  1. MainForm which is my parent form

  2. Child form which is the child of the above mentioned form

  3. 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);
}
}









share|improve this question


















  • 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






  • 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






  • 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 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





    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














0












0








0


1






I have a Winforms C# based MDI application and I have 3 Forms




  1. MainForm which is my parent form

  2. Child form which is the child of the above mentioned form

  3. 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);
}
}









share|improve this question














I have a Winforms C# based MDI application and I have 3 Forms




  1. MainForm which is my parent form

  2. Child form which is the child of the above mentioned form

  3. 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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 like ExecuteQueryAsync etc

    – Panagiotis Kanavos
    Nov 14 '18 at 11:23






  • 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






  • 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 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





    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





    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





    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





    @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 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





    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












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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly