Messagebox in form load event hangs splash screen
I have a splash screen in my application that pops up & goes away as expected when it starts up. When I add a messagebox in the form load event, the splash screen pops up & hangs the app. I'm guessing the messagebox is popping up behind the splash screen & is waiting for input. Can anyone tell me why this is happening? Why doesn't the splash screen still go away, even with the messagebox? What I want to happen is for the splash screen to pop up, go away after a few seconds & show the messagebox. The user then clicks the OK button on the messagebox & then the application form displays. Is there a way to make it work like this? Thanks...
Re: Messagebox in form load event hangs splash screen
Code:
Public Class splash
Inherits Form
Public Sub New()
Me.Text = "Splash Screen"
Me.Size = New Size(300, 200)
Me.StartPosition = FormStartPosition.CenterScreen
Me.Show()
Dim timer As New Timer()
AddHandler timer.Tick, AddressOf OnTimerTick
timer.Interval = 3000 ' 3 seconds
timer.Start()
End Sub
Private Sub OnTimerTick(sender As Object, e As EventArgs)
Dim timer As Timer = CType(sender, Timer)
timer.Stop()
timer.Dispose()
Me.Hide()
MessageBox.Show("Welcome to the Application!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Dim mainForm As New MainForm()
mainForm.Show()
End Sub
End Class
Public Class MainForm
Inherits Form
Public Sub New()
Me.Text = "Main Application"
Me.Size = New Size(800, 600)
End Sub
End Class
Re: Messagebox in form load event hangs splash screen
Quote:
Originally Posted by
nbrege
I have a splash screen in my application that pops up & goes away as expected when it starts up. When I add a messagebox in the form load event, the splash screen pops up & hangs the app. I'm guessing the messagebox is popping up behind the splash screen & is waiting for input. Can anyone tell me why this is happening? Why doesn't the splash screen still go away, even with the messagebox? What I want to happen is for the splash screen to pop up, go away after a few seconds & show the messagebox. The user then clicks the OK button on the messagebox & then the application form displays. Is there a way to make it work like this? Thanks...
Your problem is because MessageBoxes are modal. As soon as you show the MessageBox, all of your other code is stopped waiting for the MessageBox to close. There are 2 possible fixes. 1st is to show the MessageBox in a secondary thread, or 2nd, something similar to Red.x’s Timer idea…
Edit: Possibly, there’s an overload of MessageBox.Show that allows you to choose a parent or an owner, which would place it in front of the specified form…
Re: Messagebox in form load event hangs splash screen
Quote:
Originally Posted by
.paul.
Your problem is because MessageBoxes are modal. As soon as you show the MessageBox, all of your other code is stopped waiting for the MessageBox to close. There are 2 possible fixes. 1st is to show the MessageBox in a secondary thread, or 2nd, something similar to Red.x’s Timer idea…
Edit: Possibly, there’s an overload of MessageBox.Show that allows you to choose a parent or an owner, which would place it in front of the specified form…
There is an Owner overload, but when I use the splash form as the owner, it gives me cross-thread operation error. This is the code I tried:
Code:
MessageBox.Show(My.Forms.Splash1, "Test")
How would I show the messagebox in a secondary thread?
Re: Messagebox in form load event hangs splash screen
Code:
Dim t As New Thread(
Sub()
MessageBox.Show("Test")
End Sub)
t.Start()
Re: Messagebox in form load event hangs splash screen
The issue is that the splash screen won't be dismissed until the startup form is ready to be displayed but the startup form is not ready to be displayed until it's Load event handler has been executed. Displaying the message on a different thread is not the solution because then it's not modal to the startup form anyway, so it won't stop the startup form being displayed. The obvious solution is to display the message in the Shown event handler instead of Load, as long as displaying the message immediately after the startup form is displayed rather than just before is OK. If that's a problem, you simply cannot use the standard splash screen functionality and have this form be the startup form. There are ways to achieve what you want but they will require more work.
Re: Messagebox in form load event hangs splash screen
How about putting what ever is in the message box in a label on the splash screen and save the user and extra click?
Re: Messagebox in form load event hangs splash screen
I was going to go roughly in the direction Tyson talked about, but then thought better of it.
I ran into an issue that a standard splash screen wouldn't cover. In my case, the splash screen didn't stay visible long enough. In fact, the Shown event of the main form wasn't long enough, either. I needed the splash screen to remain visible (and full screen) for about two seconds after the Shown event of the main form was raised. That's not a typical splash screen, so I had to make my own...sort of. In reality, something that is "like a splash screen, but not like a splash screen" is not a splash screen.
What I did was create a full-screen window that was shown non-modally. The main form was created in the background and displayed, with the façade (splash screen) able to handle events from the main form. Of course, that façade screen was just a form, and could have whatever else you wanted on it. A spinner would be dull and misleading, so, aside from some graphical elements, the main feature of the façade screen was a message that was periodically updated to let the user know that progress was being made.
You could do something like that, with a message showing whatever you wanted while the main form is in the background...doing whatever you want it to do. You might want to be showing that message from before the Load event of the main form, too. Show it as soon as possible.