Launch winform in new thread - form wont stay open - just dissapears

  • Thread starter Thread starter D Witherspoon
  • Start date Start date
D

D Witherspoon

My application takes 5 or 6 seconds to load because of the time required to
communicate with web services and load from the database. In the meantime
I'd like to show a splash screen in a seperate thread.

I am trying the following code and it shows the form for a split second and
then dissapears. Can someone help me out with what I'm doing wrong here.

Thanks,

Dan



Public Sub Main()

Dim t As New System.Threading.Thread(AddressOf OpenSplash)

t.IsBackground = False

t.ApartmentState = Threading.ApartmentState.STA

t.Start()

'do tons of work when loading app 'cut for brevity.. 'takes 5-7 seconds
of loading time

'CloseSplash

End Sub



Public Sub OpenSplash()

fSplash = New frmSplash

fSplash.Visible = True

fSplash.Show

End Sub
 
D Witherspoon said:
My application takes 5 or 6 seconds to load because of the time required
to communicate with web services and load from the database. In the
meantime I'd like to show a splash screen in a seperate thread.

Instead of showing the window in a separate thread, show the window in the
application's main UI thread and perform the lengthly operation in a
separate worker thread. Your application is lacking a message loop for the
thread the 2nd form is shown from and thus the form will be closed
immediately.
 
D Witherspoon said:
My application takes 5 or 6 seconds to load because of the time required to
communicate with web services and load from the database. In the meantime
I'd like to show a splash screen in a seperate thread.

I am trying the following code and it shows the form for a split second and
then dissapears. Can someone help me out with what I'm doing wrong here.

Why do you want the splash screen to be in a separate thread?

Your problem is down to the fact that in order to survive a message loop is
needed to handle messages sent by windows. You would need to use
Application.Run (hence your subsequent post :-) to get the form to hang around

James
 
You need to have a timer in the form you want to display as splash screen.
Set the time you want the window to be displayed in the timer's interval
property and close the form when the tick event fires.

Regards,
 
It has to be in a seperate thread. Otherwise the windows contents of the
splash screen to not refresh quick enough for viewing pleasure. The thread
is too busy doing other application startup duties.
 
Beautiful ..

James Mahoney said:
Why do you want the splash screen to be in a separate thread?

Your problem is down to the fact that in order to survive a message loop
is
needed to handle messages sent by windows. You would need to use
Application.Run (hence your subsequent post :-) to get the form to hang
around

James
 
Or alternately, you can fire an event from Sub Main that is handled in the
Splash Thread to close the Splash thread (you would have to show the Splash
form as a .showdialog in the splash thread though.
 
Back
Top