开发者

Loading Data Table on form load, how to speed it up in c#

开发者 https://www.devze.com 2023-04-12 19:05 出处:网络
i have c# windows form when it opens it load thousands of record to fill a data table, the problem is when i click on the form it show a blank window for some time before displaying all the data, So

i have c# windows form when it opens it load thousands of record to fill a data table, the problem is when i click on the form it show a blank window for some time before displaying all the data, So

How do i display a wait sign on the datagridview while the data load and let the form开发者_高级运维's other control to be visible?

how can i speed it i am ready to do threading if it does the job.


You can absolutely use threading for that. It basically consists on starting another thread to query the database while you display some loading message to your user.

Heres some documentation:

MSDN Thread Class

And some code example:

Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();

Where ThreadProc should be your Data Loading Method.

Let me clarify a little more:

Your Data Loading method should only query the database and supply the return to the main Thread, as pointed out in comments, you cannot interact with UI elements from another Thread.


You need a background thread. You could set up a BackgroundWorker in your form, or set up a basic asynchronous model with a BeginInvoke in your event handler, which will be given another method as its callback when done. In the background worker, do all your data retrieval, and then Invoke back to the main thread when you need to actually populate the DGV (which should take much less time once the information is all in memory).

The problem is that while the main thread is busy waiting for the DB to come back with all the rows, the system cannot respond to any other user input or Windows messages (such as "redraw yourself"). Windows will see that the message queue for that app is backing up and mark the app as "not responding". Not a very pleasant user experience.


You should have to use BackgroundWorker class.


use backgroundworker component. Its easier to handle it than threading on is own Alternatively, you can use async methods provided by ado.net such as beginexecutereader etc


You can use asynchronous queries in combination with partial results.

If your database system does not support the LIMIT-clause common in MySQL to specify partial results, you can use an approach like this.

0

精彩评论

暂无评论...
验证码 换一张
取 消