2

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)
Sean P
  • 949
  • 4
  • 22
  • 41
  • the sleeps are there because i wanted to see if the functions were just processing too fast for any real drawing to happen – Sean P Jan 20 '10 at 23:53

3 Answers3

2

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
Community
  • 1
  • 1
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • put the background worker into splash or the form calling it? – Sean P Jan 21 '10 at 00:27
  • @Sean P: It works both ways, but if you move the code to the splash screen you will have to change `splash.Close()` into `Me.Close` of course. Where you put it depends on your personal preferences. Just be consistent with the rest of your projects. – Dirk Vollmar Jan 21 '10 at 00:33
0

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.

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • The functions shouldnt take too much time but one of them will take a few seconds when im done. I will try putting it in a different thread. – Sean P Jan 21 '10 at 00:22
0

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
Patrizio Bertoni
  • 2,582
  • 31
  • 43
Sean P
  • 949
  • 4
  • 22
  • 41
  • 1
    It might be because you call MakeTree immediately after calling RunWorkerAsync, RunWorkerAsync will return immediately and probably hasn't finished loading your tree etc. You need to have your code wait until it's finished, or maybe call MakeTree from the completed event? – Matt Jan 21 '10 at 01:59
  • yes, call MakeTree from the completed event or move it to the end of the DoWork method. If MakeTree is updating the UI, it is probably simplest to move it to the completed event. – Dirk Vollmar Jan 21 '10 at 09:02
  • The labels are still not updating though. What should i do with that? – Sean P Jan 21 '10 at 18:36