Martijn
Martijn

Reputation: 24789

Winforms: How to display a "loading" form?

I have a grid and when a row is double clicked a form is loaded. However a lot of data must be loaded, so I'd like to display a simple form with the text 'loading, please wait..'. And when all loading is finished, the form must disappear.

This is what I have right now, but it doesn't work:

Code that invokes the form with lots of data:

FormWithLotData form = new FormWithLotData();
form.ShowDialog(this);

Constructor of FormWithLotData:

// Show load form
FormIsLoading frm = new FormIsLoading();
_CloseLoadForm closeForm = new _CloseLoadForm(frm.Close);
System.Threading.Thread thread = new System.Threading.Thread(frm.Show);

thread.Start();

InitializeComponent();

this.Visible = false;

LoadAllData();

this.Visible = true;

// Close load form
Invoke(closeForm);

Hope you can help me out.

EDIT: I'd like to show an animated gif on the loading form.

SOLUTION: I've created a background worker. The DoWork event handles all the loading and by using the invoke() method I add nodes to the treeview. Now, the GUI doesn't hang and the user don't have the idea that the application is hanging.

Upvotes: 5

Views: 13503

Answers (5)

FELIXKIPRONO
FELIXKIPRONO

Reputation: 21

  1. Inside your form, you can use a Timer control to simulate loading using ProgressBar when it reaches 100 it unloads the form or any kind of animation.

    for progress-bar code just add the control from the toolbox and then write the following code:

ProgressBar1.Value = ProgressBar1.Value+1;
if(ProgressBar1.Value == 100)
{ 
  timer1.Enabled = false; this.Hide();
  MessageBox.Show("Complete Loading...");
}

Upvotes: 0

user1228
user1228

Reputation:

You need to reverse your code.

The constructor of FormWithLotData is running in the UI thread. This is the thread that must show your FormIsLoading form. So instead of trying to display this form using the new Thread, do your data loading with it.

The DoEvents way others have suggested is the easiest to implement and (possibly? never done it myself) may work well.

The better pattern to use is to do your data loading on a worker thread. Before you show your FormWithLotData, Begin loading data on a background thread and show your Loading dialog. The method that loads the data should have a callback method into the Loading dialog to signal when it should Close(). Once it closes you can then construct a new FWLD, pass it the already loaded data, and Show it.

Trying to load your data after the form has already been invoked mixes your UI with your data operations, forcing your form to not only be in charge of the UI but also be in charge of data retrieval. Bad for KISS and Single Responsibility, imho.


After your update, it seems like DoEvents is going to be the only real answer to your question, but with some caveats.

You will not be able to show another form MODALLY while you construct your tree. You will still have to do your heavy work within your form's constructor. You will still have to hide your main form and Show() (not ShowDialog) your loading form. You will also have to call DoEvents at every single possible moment while constructing your tree. Its not exactly an elegant solution, but it will probably be your best bet at this point.

Upvotes: 6

slugster
slugster

Reputation: 49974

Don't do your LoadAllData() directly in a UI thread, instead start up a background thread to do it. The in your Form_Loaded event handler, use an AutoResetEvent and wait till it becomes signalled by the background data retrieving thread. Once it is signalled you can then continue to do whatever you need to do with the data, like bind it into the UI.

This method is still a little clunky for various reasons, but it will get you started.

Edit: i was being lazy with my answer above... a better option is to pass a delegate (callback) to the background thread, when the delegate is invoked (upon completion of the data retrieval) it marshals itself back on to the UI thread, and starts doing the required work with the data.

Upvotes: 0

Hinek
Hinek

Reputation: 9729

how about ...

FormIsLoading frm = new FormIsLoading();
frm.Show();
Application.DoEvents();

// ... load data ...

frm.Close();

Upvotes: 5

David
David

Reputation: 73564

In the Form_Load event, add the following:

this.Visible = true;
Application.DoEvents();

before any other processing occurs. The Application.DoEvents caused the UI to show the form at the current state, where normally the UI thread is locked while you other processing is taking place.

Upvotes: 2

Related Questions