Winforms: how to display the "download" form?

I have a grid, and when the line is double clicked, the form loads. However, you need to upload a lot of data, so I would like to display a simple form with loading text, please wait .. ". And when the whole download is finished, the form should disappear.

Here is what I have right now, but this does not work:

Code that invokes a form with lots of data:

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

Form constructorWithLotData:

 // 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); 

I hope you help me.

EDIT: I would like to show an animated gif in the upload form.

SOLUTION: I created a background worker. The DoWork event handles the entire load, and using the invoke () method, I add nodes to the treeview. Now the GUI does not freeze, and the user has no idea that the application is hanging.

+4
source share
5 answers

You need to cancel your code.

The FormWithLotData constructor runs in the user interface thread. This is the stream that should show the form FormIsLoading. Therefore, instead of trying to display this form using a new stream, upload your data with it.

The method proposed by DoEvents that others have proposed is easiest to implement and (probably never did it by itself) may work well.

It is better to use a template to load data into a workflow. Before showing your FormWithLotData form, start loading the data into the background stream and display the "Download" dialog box. A method that loads data must have a callback method in the Load dialog box to signal when it should close (). After closing it, you can create a new FWLD, transfer the already downloaded data to it and show it.

Trying to load your data after the form has already been launched mixes your user interface with your data operations, making your form not only responsible for the user interface, but also responsible for finding the data. Bad for KISS and Single Responsibility, imho.


After your update, it seems that DoEvents will be the only real answer to your question, but with some caveats.

You cannot show another MODALLY shape while you are building your tree. You still have to do your hard work inside your form designer. You still have to hide your main form and Show () (not ShowDialog) your upload form. You will also need to call DoEvents at every possible moment, creating your tree. This is not an entirely elegant solution, but it will probably be your best bet at this point.

+6
source

What about...

 FormIsLoading frm = new FormIsLoading(); frm.Show(); Application.DoEvents(); // ... load data ... frm.Close(); 
+5
source

In the Form_Load event, add the following:

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

before any other processing. Application.DoEvents caused the user interface to display the form in its current state, where typically the user interface thread is blocked during other processing.

+2
source

Do not LoadAllData() your LoadAllData() directly in the user interface thread; instead, run a background thread to do this. In the Form_Loaded event handler, use AutoResetEvent and wait until it is signaled by the background data stream. Once it is signaled, you can continue to do whatever you need with the data, for example, bind it to the user interface.

This method is still a bit awkward for various reasons, but it will help you get started.

Edit: I lied with my answer above ... the best option is to transfer the delegate (callback) to the background thread when the delegate is called (after the data finishes searching), it returns to the user interface thread again and starts to do the required work with the data.

0
source
  • Inside the form, you can use the timer to simulate loading using the ProgressBar, when it reaches 100, it unloads the form or any kind of animation.

    for progress-step code, just add a control from the toolbar, and then write the following code:

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

Source: https://habr.com/ru/post/1309116/


All Articles