views:

482

answers:

5

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)
A: 

Try calling the refresh of the label itself. It may not be refreshing as the child of the splash. As silly as that sounds, I've seen it happen. I couldn't explain it then either, but that's what worked for me.

Joel Etherton
I tried that just now and it didnt work. Not sure what to do.
Sean P
+2  A: 

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
0xA3
put the background worker into splash or the form calling it?
Sean P
@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.
0xA3
I had to post an answer but my results werent good.
Sean P
A: 

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.

Chris Lively
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
A: 

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
Sean P
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
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.
0xA3
The labels are still not updating though. What should i do with that?
Sean P
Still trying to figure out why the labels are not updating....
Sean P
A: 

Anyone else have any clues as to why the labels arent updating?

Sean P