如何加载独立于数据的UI

我正在使用web服务器中的c#和数据库创建一个应用程序。当从web服务器访问数据时,它非常慢并且表单也被挂起直到数据被加载。有没有办法先加载表单,然后加载数据?

解决此问题的常用方法是使用BackgroundWorker类。

 public void InitBackgroundWorker() { backgroundWorker.DoWork += YourLongRunningMethod; backgroundWorker.RunWorkerCompleted += UpdateTheWholeUi; backgroundWorker.WorkerSupportsCancellation = true; // optional // these are also optional backgroundWorker.WorkerReportsProgress = true; backgroundWorker.ProgressChanged += UpdateProgressBar; } // This could be in a button click, or simply on form load if (!backgroundWorker.IsBusy) { backgroundWorker.RunWorkerAsync(); // Start doing work on background thread } // ... private void YourLongRunningMethod(object sender, DoWorkEventArgs e) { var worker = sender as BackgroundWorker; if(worker != null) { // Do work here... // possibly in a loop (easier to do checks below if you do) // Optionally check (while doing work): if (worker.CancellationPending == true) { e.Cancel = true; break; // If you were in a loop, you could break out of it here... } else { // Optionally update worker.ReportProgress(somePercentageAsInt); } e.Result = resultFromCalculations; // Supports any object type... } } private void UpdateProgressBar(object sender, ProgressChangedEventArgs e) { int percent = e.ProgressPercentage; // Todo: Update UI } private void UpdateTheWholeUi(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { // Todo: Update UI } else if (e.Error != null) { string errorMessage = e.Error.Message; // Todo: Update UI } else { object result = e.Result; // Todo: Cast the result to the correct object type, // and update the UI } } 

听说multithreading? 看到这个和这个例子。

如果multithreading对您来说很难,您可以先加载部分数据,然后在表单上放一些按钮让用户获取其他数据部分。 这通常称为按需加载,实现时称为分页。 想象一下,你有10000条信息记录。 您可以加载前100条记录,然后让用户只在他想要的时候加载第二条100条记录。 如果你在服务器上做了一些冗长的操作而问题不在于按需加载,那么唯一的方法是使用线程:)

您可以在UI thread中的其他thread获取数据。 这样您的UI就不会卡住。 看看这篇解释线程的post。 请记住,您无法从创建它的线程以外的线程更新控件。 要解决此问题,请查看此post 。

你使用什么样的数据?

您可以使用BackgroundWorker,或者在某些情况下使用异步方法(例如,如果您调用webservivce)