首先加载界面 [英] Loading the interface first

查看:132
本文介绍了首先加载界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C#Windows Forms应用程序.我从文件中读取了一些信息,并填充了datagridview.该程序仅在读取和填充完成(持续4-5秒)后才打开.我想要的是先加载并显示接口,然后再将信息同时加载到datagridview. (类似于Windows的add/remove-programs接口).

I am writing a C# Windows Forms application. I read some information from a file and fill a datagridview. The program opens only after the reading and filling is complete (which lasts 4-5 seconds). What I want is to load and show the interface first and later load the informations to datagridview simultaneously. (Something like add/remove-programs interface of windows) Is it even possible?

推荐答案

没有人喜欢一个似乎几秒钟没有反应的系统.

您需要的是消除Form的构造函数或其Load处理程序中所有繁琐的操作,包括显式计算以及数据绑定.您可以将它们延迟到Shown事件;即使这样,您也应该考虑将它们放在单独的线程中(也许是BackgroundWorker);这将使GUI保持活动状态(因此,当它被其他对象覆盖并未被其他对象覆盖时,它会重新绘制),但是,这会使GUI的更新更加困难.您可能需要阅读 [
Of course it is possible, and it is the way it should be done; nobody likes a system that seems not to react for several seconds.

What you need is eliminate all long-winding operations from the Form''s constructor or its Load handler, including explicit calculations as well as data bindings. You could delay them till the Shown event; and even then you should consider putting them in a separate thread (maybe a BackgroundWorker); that would make the GUI alive (so it repaints when it gets covered and uncovered by other objects, etc), however it makes updating the GUI slightly more difficult. You may want to read this[^].

:)


是的,您必须做三件事

1)产生后台线程进程
请参见
http://support.microsoft.com/kb/815804 [
yes you have to do 3 things

1) spawn off a background thread process
see http://support.microsoft.com/kb/815804[^] for an example

2) in the background thread load your file

3) once the data is loaded you have to marshall a method back to your main thread and then bind your data to your datagridview.
This would be something like
private void BindData()
{
  If (myDataGrid.InvokeRequired)
  {
    myDataGrid.BeginInvoke(BindData)
  }

  ... code to do binding
  myDataGrid.DataSource = ...
}



默认情况下,我也会禁用datagridview,只有在有数据绑定到它时才启用它.

请参阅示例
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx [



I''d also disable the datagridview by default and only enable it once you have data to bind to it.

see example http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx[^]


这很有可能……尽管并不总是那么容易,这取决于您想要什么与之相关.我正在用其中一种表格做类似的事情.

我有一系列的ComboBox es从数据库中的数据中填充.数据库需要一段时间才能返回数据,所以我在ComboBox es前面有一个PictureBox,上面有一个简单的Loading gif,这是在我第一次进行呼叫以填写框时显示的.当盒子完成加载后,我只需隐藏PictureBox.

因此,我执行此操作的实际方法很复杂.基础是我使用BackgroundWorker从数据库中加载数据. BackgroundWoker完成后,它引发一个我已钩住的事件,告诉我可以隐藏PictureBox并启用ComboBoxes.

它比包含Invoke/InvokeRequired和几个事件的过程要复杂得多,但这是主要思想.

我考虑写一篇关于我正在做的事情的文章,作为一个示例,说明如何在部分BackgroundWorker完成后如何使用BackgroundWorker加载不同的控件.
It''s very possible...though not always easy, depending on what all you want to do with it. I''m doing something similar with one of my forms.

I have a series of ComboBoxes that get filled in from data in a database. It takes a while for the database to return the data, so I have a PictureBox in front of the ComboBoxes with a simple Loading gif that is shown when I first make the call to fill in the boxes. When the boxes are finished loading, I simply hide the PictureBox.

So, the real way that I do it is complicated. The basics are that I use a BackgroundWorker to load the data from the database. When the BackgroundWoker is complete, it raises an event that I have hooked that tells me that I can hide the PictureBox and enable the ComboBoxes.

It''s quite a bit more complicated than that including Invoke/InvokeRequired and several events, but that''s the main idea.

I''ve considered writing up an article about what I''ve been doing just as an example of how you can use BackgroundWorker''s to load different controls when parts of the BackgroundWorker have completed.


这篇关于首先加载界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆