0

In my form_load I have a mysql connection that loads data in a datagrid, when starting my application it will first try to load the mysql database and after its done it will load the form/interface with all the controls.

This leaves the user the impression nothing is happening, how can I first load the form/interface with all the controls and then my mysql database?

4

5 回答 5

1

Move your code to Shown event or OnShown overridden method. this will allow your form shown to user before it loads grid.

You should move your "Database calls" to worker threads as well.

protected override void OnShown(EventArgs e)
{
    //Delegate the DB calls to worker threads here 
    //everything will be smooth then..
    base.OnShown(e);
}
于 2013-10-17T21:12:57.920 回答
1

There are two ways to approach this situation. You need to pick the approach that best suits your needs.

Approach 1: Create a splash screen. A splash screen is a small window that opens up and usually had product info and a message like "Initializing...". You can see this in a lot of applications, such as Microsoft Office. You can see how to make one by looking at this article.

Approach 2: You will need to make your load data happen in a separate thread then your UI. You will need to load your data in the separate thread and bind it to a list of some sort temporarily, then after its done, bind the list to your Datagrid

于 2013-10-17T21:14:01.430 回答
1

I would say, 3 approaches

1 - Separate Splash screen

2 - On-Form "splash screen" - first thing you do on form.load, is Me.Show(), then show a message, something like "Please wait while data is loaded". Progress can be shown to users as well using Application.DoEvents which will refresh your screen.

3 - Background worker thread. Using it, you can actually give user progress updates. That will leave your form completely responsive. http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx

于 2013-10-17T22:10:28.497 回答
0

Use other thread for loading data from mysql. It's loading form first, but not displaying it until code has finished work.

new Thread(new ThreadStart(LoadDataMethodHere)).Start();
于 2013-10-17T21:10:50.717 回答
0

You can use Form Shown event.

Alternatively you can use a different thread to connect to database and show data or use a timer.

于 2013-10-17T21:11:50.447 回答