Results 1 to 4 of 4

Thread: [RESOLVED] Splash Screen Best Way

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Resolved [RESOLVED] Splash Screen Best Way

    Hello

    Need some help here, i don't know the best way to do what i want.

    I need that my application have a splash screen, this splash must update a label with messages that i sent from the main form constructor, in the end of the constructor i need to close the splash, the splash must accept key presses, so if the user press some keys I'll show the login screen, by default the login it's the same of the machine, and this is useful for debugging/tests purposes...

    Basically i need to send text to another thread, and get a boolean value from the same thread to know if the user pressed the correct combination of keys to show the login form.

    How can i do this?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  2. #2
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Salt Lake City
    Posts
    313

    Re: Splash Screen Best Way

    like this:

    in the splash form

    Code:
     Delegate Sub SetTextCallback(ByVal msg As String)
    
        Public Sub UpdateWorkLabel(ByVal msg As String)
    
            If lblWork.InvokeRequired Then
                ' It's on a different thread, so use Invoke.
                Dim d As New SetTextCallback(AddressOf UpdateWorkLabel)
                Invoke(d, New Object() {msg})
            Else
                if msg = "HIDEME" Then
                    Hide()
                    Close()
                Else
                    ' It's on the same thread, no need for Invoke.
                    lblWork.Text = msg
                End If
    
            End If
    
            Application.DoEvents()
        End Sub
    in main form

    Code:
    Dim fs As New frmSplash
    Dim t As New Thread(AddressOf fs.ShowDialog)
    t.IsBackground = True
    t.Priority = ThreadPriority.AboveNormal
    t.Name = "Splash form"
    t.Start()
    and then any time in mainform_load you can do something like this:

    Code:
    fs.UpdateWorkLabel("Processing foo")
    <do some work>
    fs.UpdateWorkLabel("Processing bar")
    <do some work>
    when you are all done loading and want to hide the splash screen, do this:

    Code:
    fs.UpdateWorkLabel("HIDEME")
    extending that to add a boolean flag is easy enough: just declare it and check it whenever you need to in your main form and act appropriately
    Last edited by EricPeterson87; Jan 11th, 2012 at 08:54 AM. Reason: code cleanup

  3. #3

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Splash Screen Best Way

    Thanks
    I almost solved the problem with similar approach the only thing that i didn't use was the delegate, the final code that i'm using:
    Splash Form:
    vb.net Code:
    1. Public Class MySplash
    2.     Public Event KeyPressed()
    3.     Delegate Sub SetTextCallBack(ByVal message As String)
    4.  
    5.     Public Sub UpdateMessage(ByVal message As String)
    6.         If LabelMessage.InvokeRequired Then 'Different thread
    7.             Dim d As New SetTextCallBack(AddressOf UpdateMessage)
    8.             Invoke(d, New Object() {message})
    9.         Else
    10.             LabelMessage.Text = message
    11.         End If
    12.     End Sub
    13.  
    14.  
    15.     Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    16.         If e.Shift Then RaiseEvent KeyPressed()
    17.     End Sub
    18. End Class

    MainForm Code:
    vb.net Code:
    1. Public Class MainForm
    2.     Private splash As MySplash
    3.     Private threadSP As Threading.Thread
    4.  
    5.     Private Sub ShowLoginForm()
    6.         If LoginForm.ShowDialog <> Windows.Forms.DialogResult.OK Then
    7.             MsgBox("Login Failed", MsgBoxStyle.Information)
    8.             End
    9.         End If
    10.         threadSP.Abort()
    11.     End Sub
    12.  
    13.     Public Sub New()
    14.         splash = New MySplash
    15.         splash.TopMost = True
    16.         threadSP = New Threading.Thread(New Threading.ThreadStart(AddressOf splash.ShowDialog))
    17.         AddHandler splash.KeyPressed, AddressOf ShowLoginForm
    18.  
    19.         threadSp.Start()
    20.  
    21.         'Task1
    22.         splash.UpdateMessage("Task1........")
    23.         Threading.Thread.Sleep(2000)
    24.         'Task2
    25.         splash.UpdateMessage("Task2........")
    26.         Threading.Thread.Sleep(2000)
    27.         'Task3
    28.         splash.UpdateMessage("Task3........")
    29.         Threading.Thread.Sleep(2000)
    30.  
    31.         ' This call is required by the designer.
    32.         InitializeComponent()
    33.         ' Add any initialization after the InitializeComponent() call.
    34.  
    35.         threadSp.Abort()
    36.     End Sub
    37. End Class
    Last edited by mickey_pt; Jan 11th, 2012 at 10:34 AM. Reason: Public > Private

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  4. #4
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Salt Lake City
    Posts
    313

    Re: [RESOLVED] Splash Screen Best Way

    excellent

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width