我制作了一个启动画面,并希望在程序的不同部分加载时对其进行一些文本更改,但是当我使用刷新或更新时屏幕不会更新。
Dim splash As New BMSSplash
splash.Show()
splash.lblStatus.Text = "Retrieving active users..."
splash.Refresh()
buddyList.setuserList()
System.Threading.Thread.Sleep(5000)
splash.lblStatus.Text = "Retrieving bonder info..."
splash.Refresh()
GetBonderGeneralAndDeviceList(CurrentBonderSetup)
System.Threading.Thread.Sleep(5000)
splash.Close()
MakeTree(CurrentBonderSetup)
答案 0 :(得分:2)
您最好在后台线程中执行所有初始化任务。这将使您的UI保持响应。
这个answer相关问题在C#中有一些示例代码。
对于该任务使用BackgroundWorker
可能最简单(只需将BackroundWorker组件拖到表单上)即可。使用BackgroundWorker,您还可以轻松报告已完成的初始化百分比,例如显示在进度条中。
Public Class Form1
Private splash As BMSSplash
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
splash = New BMSSplash
splash.Show()
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object,
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim percentageCompleted As Integer
BackgroundWorker1.ReportProgress(percentageCompleted,
"Retrieving active users...")
' replace the sleeps with the longer-running init task
System.Threading.Thread.Sleep(5000)
BackgroundWorker1.ReportProgress(percentageCompleted,
"Retrieving bonder info...")
System.Threading.Thread.Sleep(5000)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object,
ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim message As String = e.UserState
splash.lblStatus.Text = message
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
splash.Close()
End Sub
End Class
答案 1 :(得分:0)
如果“启动”屏幕显示和隐藏得非常快,那么我建议完全摆脱它。
如果您预计这些功能需要一些时间,那么您最好不要加载基本应用程序并允许用户尽快进入。然后假脱机不同的线程以根据需要加载其他资源。
例如,显示一个有点空的屏幕,然后在加载数据时开始显示您的好友列表。粘合装置列表也是一样。
这使得用户认为您的应用运行速度比实际运行速度快得多。
答案 2 :(得分:0)
我这样做了它并没有奏效。实际上现在我的GetBonderGeneralAndDeviceList
功能无法正常工作,
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
splash = New BMSSplash
splash.Show()
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
MakeTree(CurrentBonderSetup)
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim percentageCompleted As Integer
percentageCompleted = 30
BackgroundWorker1.ReportProgress(percentageCompleted, "Retrieving active users...")
buddyList.setuserList()
System.Threading.Thread.Sleep(5000)
percentageCompleted = 70
BackgroundWorker1.ReportProgress(percentageCompleted, "Retrieving bonder info...")
GetBonderGeneralAndDeviceList(CurrentBonderSetup)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim message As String = e.UserState
splash.lblStatus.Text = message
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
splash.Close()
End Sub