I made a splash screen and want to have some text change on it as different parts of the program are loaded but the screen isnt updating when i use refresh or update.
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)
You are best doing all the initialization tasks in a background thread. This will keep your UI responsive.
This answer to a related question has some sample code in C#.
It might be easiest to use a BackgroundWorker
for that task (simply drag the BackroundWorker component onto your form). Using a BackgroundWorker you can also easily report the percentage of the initialization that is already done, e.g. to be displayed in a progress bar.
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
divo had a good suggestion. However, one thing stands out in your original question.
If the "splash" screen is displaying and hiding very quickly then I'd suggest getting rid of it altogether.
If you forsee those functions taking a bit of time then you are far better off loading the base app and allowing the user to get in as quickly as possible. Then spool off different threads to load additional resources as necessary.
For example, show a somewhat empty screen then have your buddy list start appearing as the data is loaded. Same thing for the Bonder Device List.
This results in much happier users who think your app runs faster than it actually does.
I did this and it didnt work. Actually now my GetBonderGeneralAndDeviceList
function does not work properly,
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
精彩评论