Diplaying a Load message while main form is Loading

Posted on 2005-04-15
Last Modified: 2011-09-20
My app has a sub, which calls the Login form where my users need to enter Login Info to be able to access the application,
once they pass it opens the Main Window of the App. All that works fine except for the factm that the mainform loads a
large set of data on load which can cause a 10 - 30 sec delay until the form is displayed, depending on serverload and
connection speed.
What i would like to do is open a temp window which will display a message telling the user to
wait while the mainform is beeing loaded, then when the mainform is loaded i want to dispose this form.

i am looking for some samples or help where to hook in the load and dispose of this form or if there are better ways to do this

Question by:AlexPonnath
    LVL 12

    Expert Comment

    I'd say that when your main form is loaded... put the code that loads the dataset into a seperate thread maybe... start that off... then right on the next line, display a modal form saying "Please Wait"... remove the close buttons and such.

    Make sure the wait form is declared at class level (I think that's the correct name for it... i.e. outside of a sub/method)  so that when the other thread has completed loading the data, it can close the form.  I'll test this works in a moment... and post up some code to work with
    LVL 12

    Accepted Solution

    Ok... here's a crude "Waiting" form


    Public Class frmWait
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Label1 As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Label1 = New System.Windows.Forms.Label
            Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 16.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.Label1.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
            Me.Label1.Location = New System.Drawing.Point(0, 0)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(296, 120)
            Me.Label1.TabIndex = 0
            Me.Label1.Text = "LOADING"
            Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.BackColor = System.Drawing.SystemColors.ActiveCaption
            Me.ClientSize = New System.Drawing.Size(292, 120)
            Me.ControlBox = False
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "frmWait"
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            Me.Text = "frmWait"

        End Sub

    #End Region
    End Class


    Now, in your main form after logging in, I've put this in the Load event, you might change this for wherever the main form starts loading data...

        Private frmWaiting As frmWait

        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim thdDataLoad As New Threading.Thread(AddressOf LoadingData)
            frmWaiting = New frmWait
        End Sub

        Private Sub LoadingData()
            Threading.Thread.CurrentThread.Sleep(5000)     ' This was just for testing... this would obviously be replaced by your code that loads all the data
        End Sub
    LVL 12

    Expert Comment

    Give the other experts a chance to put in their different ideas... they probably have better ones, or may find that mine is not a "good" way of doing things.
    LVL 12

    Expert Comment

    you may want to set the borderstyle of the Waiting form to "none" so that the user can't move it around the screen.

    Also set the cursor of the Waiting form to the hour glass or pointer/hourglass (app starting)

    Obviously, you can customize the waiting for to fit your style... maybe some sort of animation... up to you
    LVL 25

    Expert Comment

    Your method looks fine to me s-twilley :-)
    LVL 12

    Expert Comment

    Thanks... Im still not 100% confident with doing multi-threaded apps... especially when working other two classes. I've started using delegates and such, but they've all been pretty simple so I've not really come across situations where problems might arise across different threads
    LVL 14

    Assisted Solution

    My code for the wait form -- also use a second thread, different structure in coding:

        Dim t1 As Threading.Thread
        Dim f As frmWait

        Private Sub newForm()

            f = New frmWait

        End Sub

        t1 = New Threading.Thread(AddressOf newForm)

        myDataAdapter.Fill(myDataSet)  ' Your code for loading data

        f = Nothing
        t1 = Nothing

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now