我有很多Splash Form,MDIParent Form和其他形式。我的场景流程如下:Project Statup - SPlash Form,After Splash Form第一次显示并检查一些文件等,检查后将显示MDIParent1并且splash表单将自动关闭。
以下是我在Splash表单中的代码:
Public Class frmSplash
Dim m_CountTo As Integer = 0 ' How many time to loop.
Private Sub My_BgWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles My_BgWorker.DoWork
For i As Integer = 0 To m_CountTo
' Has the background worker be told to stop?
If My_BgWorker.CancellationPending Then
' Set Cancel to True
e.Cancel = True
Exit For
End If
System.Threading.Thread.Sleep(100) ' Sleep for 1 Second
' Report The progress of the Background Worker.
My_BgWorker.ReportProgress(CInt((i / m_CountTo) * 100))
SetLabelText_ThreadSafe(Me.lblPercent, FormatPercent(i / m_CountTo, 2))
If i = 10 Then
SetLabelText_ThreadSafe(Me.lblMessages, "Initializing..")
ElseIf i = 40 Then
SetLabelText_ThreadSafe(Me.lblMessages, "Checking Mysql Service..")
ElseIf i = 50 Then
If CheckIfServiceIsRunning("MySql") = False Then
' Is the Background Worker do some work?
If My_BgWorker.IsBusy Then
'If it supports cancellation, Cancel It
If My_BgWorker.WorkerSupportsCancellation Then
' Tell the Background Worker to stop working.
My_BgWorker.CancelAsync()
End If
End If
SetButton_ThreadSafe(Me.btnExit, True)
Exit Sub
End If
ElseIf i = 70 Then
SetLabelText_ThreadSafe(Me.lblMessages, "Checking Internet Connection..")
ElseIf i = 80 Then
If CheckURL("http://www.google.com") = False Then
SetLabelText_ThreadSafe(Me.lblMessages, "No Internet Connection..")
End If
Next
End Sub
Private Sub My_BgWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles My_BgWorker.ProgressChanged
' Update the progress bar
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub My_BgWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles My_BgWorker.RunWorkerCompleted
If e.Cancelled Then
Me.lblMessages.Text = "Cancelled"
Else
Me.lblMessages.Text = "Completed"
My_BgWorker.CancelAsync()
Me.Close()
MDIParent1.Show()
End If
End Sub
Delegate Sub SetLabelText_Delegate(ByVal [Label] As Label, ByVal [text] As String)
' The delegates subroutine.
Private Sub SetLabelText_ThreadSafe(ByVal [Label] As Label, ByVal [text] As String)
' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If [Label].InvokeRequired Then
Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
Me.Invoke(MyDelegate, New Object() {[Label], [text]})
Else
[Label].Text = [text]
End If
End Sub
Delegate Sub SetButton_Delegate(ByVal [Button] As Button, ByVal [visible] As Boolean)
' The delegates subroutine.
Private Sub SetButton_ThreadSafe(ByVal [Button] As Button, ByVal [visible] As Boolean)
' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If [Button].InvokeRequired Then
Dim MyDelegate As New SetButton_Delegate(AddressOf SetButton_ThreadSafe)
Me.Invoke(MyDelegate, New Object() {[Button], [visible]})
Else
[Button].Visible = [visible]
End If
End Sub
Private Sub frmSplash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set the count to 100
m_CountTo = 100
' Start the Background Worker working
My_BgWorker.RunWorkerAsync()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
' Is the Background Worker do some work?
If My_BgWorker.IsBusy Then
'If it supports cancellation, Cancel It
If My_BgWorker.WorkerSupportsCancellation Then
' Tell the Background Worker to stop working.
My_BgWorker.CancelAsync()
End If
End If
' Enable to Start Button
Me.btnExit.Enabled = True
' Disable to Stop Button
Me.btnExit.Enabled = False
Application.Exit()
End Sub
结束班
任何人都可以帮助我,在完成初始化表单加载之后,最好的方法是在子RunWorkerCompleted中显示MDIParent(主窗体)。 在上面的代码中,MDIParent1无法正确显示,因为在show之后,应用程序已关闭/终止。
答案 0 :(得分:0)
在项目中 - >属性,将您的MDIParent设置为“启动表单”,并将您的启动表格设置为“启动表单”。
现在使用任何线程或BackgroundWorkers将所有初始化检查放在MDIParent 的Load()事件中,不带。只要Load()事件完成,Splash Form就会显示,然后自动关闭。
要更新MDIParent的启动,请将My.Application.SplashScreen转换为您的启动窗体类型,并使用委托/调用(),就像您现在一样。
这是一个愚蠢的例子:
Public Class frmMdiParent
Private Sub frmMdiParent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' initialization...
Dim splash As frmSplash = DirectCast(My.Application.SplashScreen, frmSplash)
Dim numberSteps As Integer = 10
For i As Integer = 1 To numberSteps
splash.Invoke(Sub()
splash.Label1.Text = "Step " & i & " of " & numberSteps
End Sub)
System.Threading.Thread.Sleep(1000)
Next
' some other stuff...
Dim child As New Form
child.MdiParent = Me
child.Text = "Some MdiChild..."
child.Show()
End Sub
End Class