Situation:

  1. When you start the program from the Internet, an XML file is loaded with N elements (and the properties of each inside)
  2. Using XML, we retrieve the data, and based on them we dynamically create N Panel objects. When creating each Panel, additional data is downloaded from the Internet.
  3. After creation, each Panel is sent to the form (to be more precise, to the FlowLayoutPanel container).

Because of this, the launch of the application (the appearance of the form) takes 6-7 seconds, which is unacceptable.

I tried to make the whole function in a separate thread, but the compiler swears that you cannot create a child object in a container created in another thread.

What to do?

  • 2
    Well, read the data in another thread, throw it into the main one, create controls on their basis. - VladD

1 answer 1

You need to use the Invoke method:

 void FillFLP(List<Panel> panels) { //заполнение Вашего FlowLayoutPanels } void LoadData() { List<Panel> panels = new List<Panel>(); //загрузка данных в panels this.Invoke(new Action<List<Panel>>(FillFLP), panels); } //а где-нибудь в Form_Load добавляете этот код Task task = new Task(LoadData); task.Start(); 
  • Thank you very much, I will try at home and accomplish your goal. - Mrtaracqk