splash screen & threats

Salat

Regular
Joined
Mar 5, 2002
Messages
81
Location
Poland, Silesia
I'm useing sucha a code for splash screen, which is shown while the main form is loading. There is a panel which is fluently flickering, as a progress of loading. The 'sta' integer is used to wait for splash screen to show properly.

Here is how it works:
- the splash screen is shown, the second of time is remebered by sta
- is Now.Second is other then at the start, then the Form1Loading in parent class is called
- form this form1loading, there is a new threat created and it is run
- from form1loading at class ThreadForm1Loading there is a Form1 loaded, and shown
- if it all is done, the program ends
Visual Basic:
Imports System.Threading
Public Class Form4
    Inherits System.Windows.Forms.Form

    Dim LoadingProgress As Integer = 120
    Dim LoadingProgressDirection As Boolean

    Dim sta As Double
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Application.DoEvents()
        Dim i As Integer = LoadingProgress
        Panel1.BackColor = Color.FromArgb(i, i, i)
        If LoadingProgressDirection = True Then LoadingProgress = i + 20
        If LoadingProgressDirection = False Then LoadingProgress = i - 20
        If LoadingProgress > 255 Then LoadingProgress = 255 : LoadingProgressDirection = False
        If LoadingProgress < 120 Then LoadingProgress = 120 : LoadingProgressDirection = True
    End Sub

    Public Sub Form4_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        sta = Now.Second
        Timer1.Enabled = True
        Call Form1Loading()
    End Sub

    Private Sub Form1Loading()
        Do While sta = Now.Second
            Application.DoEvents()
        Loop
        Dim oThread As New ThreadForm1Loading()
        oThread.mi = Me
        Dim otter As New ThreadStart(AddressOf oThread.Form1Loading)
        Dim oThr As New Thread(otter)
        oThr.Start()
    End Sub

    Class ThreadForm1Loading
        Public mf As Form1 = Nothing
        Public mi As Form = Nothing
        Sub Form1Loading()
            mf = New Form1()
            Application.DoEvents()
            mf.Show()
            mi.Visible = False
            Do
                Application.DoEvents()
                If mf.Visible = True Then Exit Do
            Loop
            mi.Visible = False
        End Sub
    End Class
End Class

Is it possible to load Form1 from other thread and do not end programm executing?

thanks for help...
 
I've changed this code:
Visual Basic:
        Sub Form1Loading()
            mf = New Form1()
            Application.DoEvents()
            mf.Show()
            mi.Visible = False
            Do
                Application.DoEvents()
                If mf.Visible = True Then Exit Do
            Loop
            mi.Visible = False
        End Sub

in to this:
Visual Basic:
        Sub Form1Loading()
            mf = New Form1()
            Application.DoEvents()
            mf.Show()
            mi.Visible = False
            Do
                Application.DoEvents()
            Loop
        End Sub
so the sub form1loading ends only when END statement is called.
but... the cpu useage by this program is 70-80 % it's too much.

please help me guys, I realy don't know what to do with this...
 
ok, I've changed the mf.show to mf.showdialog, and it looks to work fine, but I'm wondering if it will cause any unexpected expections in the future ;)

What do You think?
 
To save me typing the whole of this solution in, I am going to post an implementation instead.

.NET Solutions Toolkit ,
Splash Screen is contained within Chapter 01 Source.

To make the splash screen work correctly its needs to run a worker thread, separate from the main thread. A form can create a thread, but the form cannot run, on a thread it creates.

In order to get a form to run a thread, the thread needs to be created first, and then the form instantiated on that thread the SplashScreenController class is used to do this within the download.
 
Hey Salat,

I think I had the same problem as you have. My Splash screen was flikkering as well. On top of my Splash screen needs to come information about what the program is checking and setting during the starting up period.

In my form_load of the first screen that has to be shown after starting up I do a call to a Sub with name Splashscreen() the code in that sub is as follows:

Public Sub Splashscreen()
Dim Splash As Form = New frmSplash()
Splash.ControlBox = False
Splash.Show()
System.Windows.Forms.Application.DoEvents()

Splash.Text = "Find Server ..."
System.Windows.Forms.Application.DoEvents()
FindServer()

Splash.Text = "Connecting with databases..."
System.Windows.Forms.Application.DoEvents()
ConnectToCafcaSQL()
ConnectToQuallityManagementSQL()
ConnectToTemplates()

Splash.Text = "Checking Securitylevel..."
System.Windows.Forms.Application.DoEvents()
CheckSecurityLevel()
Splash.Text = "Checking parameters..."
System.Windows.Forms.Application.DoEvents()
Parameters()

Splash.Close()
End Sub

This solved my problem. I hope you can use something of this. I also declared my main screen as a Public screen. I don't know anymore this was necessary or not. Give it a try.
 
correct me if i'm wrong, boes

You have a main form, which is set as starup form, and in it's form_load event You call SplashScreen... it's right, if Your application have to do connect to databases and do the other things,

My splash screen is showing the progress of loading mainform, so the starup form is my splash screen form. In it's code, after it is whole shown, it starts to load main form of my application. if I want to load my main form from splash screen class, the executiing of it's code is suspended till the main form is loaded (progress of loading is not shown properly). So I put other class, and a new threat in splash form code, which will load the main form. (Showing of loading progress is not disturbed). My bigest problem was, that after loading main form from this class executing of my program was ended. So i decide to use ShowDialog after loading and this solve it
 
Back
Top