Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display something like "loading.." while I do some checks on form load?

Tags:

c#

.net

events

I do some checks on form load, but it is locking the form for a period (some thousandths of seconds). For this reason, I want to display a message such as "loading application..", but I have no idea how I do this. I hope this clear! Any help is very appreciated. Thanks in advance.

like image 841
The Mask Avatar asked Jan 14 '12 19:01

The Mask


4 Answers

Ideally what you want to do is to perform your checks on a background thread, so that the UI thread isn't blocked. Have a look at the BackgroundWorker class.

You should hook your checks to the DoWork event of the background worker, and call the BackgroundWorker's RunWorkerAsync() method from the Form_Load event to kick off the background work.

Something like this (note, this is untested):

BackgroundWorker bw = new BackgroundWorker();
public void Form_Load(Object sender, EventArgs e) {
    // Show the loading label before we start working...
    loadingLabel.Show();
    bw.DoWork += (s, e) => {
       // Do your checks here
    }
    bw.RunWorkerCompleted += (s, e) => { 
       // Hide the loading label when we are done...
       this.Invoke(new Action(() => { loadingLabel.Visible = false; })); 
    };
    bw.RunWorkerAsync();
}
like image 140
Chris Shain Avatar answered Oct 21 '22 07:10

Chris Shain


You can create another thread to display the loading message.

First you need a bool.

bool loading = true;

Create a thread like:

Thread myThread = new Thread(new ThreadStart(Loading));
myThread .Start();

Then have a method:

private void Loading()
{
    while(loading)
    {
        //Display loading message here.
    }
}

When you are done loading whatever just set loading to false and the tread will terminate.

like image 38
Axis Avatar answered Oct 21 '22 06:10

Axis


Have a look at BackgroundWorker component. You dont need any threading knowledge at all.

like image 32
Shiplu Mokaddim Avatar answered Oct 21 '22 08:10

Shiplu Mokaddim


This can be accomplished easily by displaying a separate form executed on another thread. In this form (call it frmSplash) you can put an animated gif or static text. The code you will need is as follows in your main form:

Declare some variables after the class.

public partial class frmMain : Form
{
   public Thread th1;
   static frmSplash splash;
   const int kSplashUpdateInterval_ms = 1;

// Rest of code omitted

Then add the following method to your main form. This starts the splash screen:

static public void StartSplash()
{
    // Instance a splash form given the image names
    splash = new frmSplash(kSplashUpdateInterval_ms);

    // Run the form
    Application.Run(splash);
} 

Next, you need a method to close the splash screen:

private void CloseSplash()
{
    if (splash == null)
        return;

    // Shut down the splash screen
    splash.Invoke(new EventHandler(splash.KillMe));
    splash.Dispose();
    splash = null;
} 

Then, in your main Form Load, do this:

private void frmMain_Load(object sender, EventArgs e)
{
    try
    {
        Thread splashThread = new Thread(new ThreadStart(StartSplash));
        splashThread.Start();

        // Set the main form invisible so that only the splash form shows
        this.Visible = false;

        // Perform all long running work here. Loading of grids, checks etc.
        BindSalesPerson();
        BindCustomer();
        BindBrand();

        // Set the main form visible again
        this.Visible = true;
    }
    catch (Exception ex)
    {
        // Do some exception handling here
    }
    finally
    {
        // After all is done, close your splash. Put it here, so that if your code throws an exception, the finally will close the splash form
        CloseSplash();
    }

} 

Then, if the main form is closed, make sure your splash screen is closed also:

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    // Make sure the splash screen is closed
    CloseSplash();
    base.OnClosing(e);
} 

The code for the Splash form (In frmSplash.cs) is as follows:

public partial class frmSplash : Form
{

    System.Threading.Timer splashTimer = null;
    int curAnimCell = 0;
    int numUpdates = 0;
    int timerInterval_ms = 0;

    public frmSplash(int timerInterval)
    {
        timerInterval_ms = timerInterval;

        InitializeComponent();
    } 


    private void frmSplash_Load(object sender, EventArgs e)
    {
        this.Text = "";
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.ControlBox = false;
        this.FormBorderStyle = FormBorderStyle.None;
        this.Menu = null;
    } 

    public int GetUpMilliseconds()
    {
        return numUpdates * timerInterval_ms;
    }

    public void KillMe(object o, EventArgs e)
    {
        //splashTimer.Dispose();

        this.Close();
    }        
}

I hope this helps you. It might not be the best code ever written, but it worked for me.

like image 37
Dirk Strauss Avatar answered Oct 21 '22 06:10

Dirk Strauss