我有一个 mdi 父窗体,当单击屏幕上的特定按钮时,会打开一个新的子窗体。当这个表单加载时,它需要调用数据库来检索信息,问题是表单需要很多时间来加载,比如15秒,加载子中没有太多代码,另外,我尝试删除来自加载事件的数据库代码,虽然它确实使其速度更快了一些,但加载仍然需要一些时间,有没有办法解决这个问题?
这是我的表格
这是我的代码
Public Class Deposito
Dim FormPadre As MDICajero = New MDICajero
Private monedas As ArrayList = New ArrayList
Private Sub Deposito_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
FormPadre = Me.MdiParent
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim ProximoForm As MenuPrincipal = New MenuPrincipal
FormPadre.LimpiarForm()
ProximoForm.MdiParent = FormPadre
ProximoForm.WindowState = FormWindowState.Maximized
ProximoForm.Show()
ProximoForm.CargaMenu(1)
End Sub
Private Sub BDepositar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BDepositar.Click
Dim dato As Emovimientos = New Emovimientos
dato.monto = montodeposito.Text
dato.idcuenta = nrocuenta.Text
dato.fecha = fechacobro.Value
End Sub End class
我以前使用过 java,从未见过表单之间有如此多的加载时间,即使是从数据库检索信息时也是如此。
编辑:在构造函数中添加了几行以使表单加载速度更快,但是,渲染过程仍然有问题。
Sub New()
Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.UserPaint, True)
InitializeComponent()
End Sub
编辑2:在MDI父窗体上,我使用了在将MDI窗体添加到解决方案时自动编写的相同代码,我添加的唯一内容是按下按钮打开子窗体时的事件:
Private Sub BRetiro_Click(sender As System.Object, e As System.EventArgs) Handles BRetiro.Click
Dim ProximoForm As Retiro = New Retiro
Me.LimpiarForm()
ProximoForm.MdiParent = Me
ProximoForm.WindowState = FormWindowState.Maximized
ProximoForm.Show()
End Sub
这是“LimpiarForm”函数的代码:
Public Sub LimpiarForm()
'borro todos los form hijos para no ocupar memoria, debe ser lo primero que tengo que hacer
Dim num As Integer
For num = 0 To Me.MdiChildren.Count - 1
Dim a = Me.MdiChildren(num)
a.Dispose()
Next
End Sub
我个人会使用多线程或后台工作者。线程内的多线程将极大地帮助您的加载时间,前提是您的线程不会遇到相同的方法。 IsPrime 是有益的,在 C++ 中用于表单加载。这是一个例子:
Private Function IsPrime(n As Long) As Boolean
For i As Integer = 2 To Math.Sqrt(n)
If n Mod i = 0 Then
Return False
End If
Next
For i As Integer = 0 To 100
Next
Return True
End Function