[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?
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
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:
Public Class MySplash
Public Event KeyPressed()
Delegate Sub SetTextCallBack(ByVal message As String)
Public Sub UpdateMessage(ByVal message As String)
If LabelMessage.InvokeRequired Then 'Different thread
Dim d As New SetTextCallBack(AddressOf UpdateMessage)
Invoke(d, New Object() {message})
Else
LabelMessage.Text = message
End If
End Sub
Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Shift Then RaiseEvent KeyPressed()
End Sub
End Class
MainForm Code:
vb.net Code:
Public Class MainForm
Private splash As MySplash
Private threadSP As Threading.Thread
Private Sub ShowLoginForm()
If LoginForm.ShowDialog <> Windows.Forms.DialogResult.OK Then
MsgBox("Login Failed", MsgBoxStyle.Information)
End
End If
threadSP.Abort()
End Sub
Public Sub New()
splash = New MySplash
splash.TopMost = True
threadSP = New Threading.Thread(New Threading.ThreadStart(AddressOf splash.ShowDialog))
AddHandler splash.KeyPressed, AddressOf ShowLoginForm
threadSp.Start()
'Task1
splash.UpdateMessage("Task1........")
Threading.Thread.Sleep(2000)
'Task2
splash.UpdateMessage("Task2........")
Threading.Thread.Sleep(2000)
'Task3
splash.UpdateMessage("Task3........")
Threading.Thread.Sleep(2000)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
threadSp.Abort()
End Sub
End Class
Re: [RESOLVED] Splash Screen Best Way