Wait until the form is completed.

Is there some kind of boolean that I can use to check if the form instance is loaded, or else wait for the form to load?

eg:

While(form_loaded == false) { Try { //do something } catch { }//do try catch so code won't barf } 

I get the following exception:

The first random error like "System.InvalidOperationException" occurred in System.Windows.Forms.dll

An unhandled exception of type "System.InvalidOperationException" occurred in System.Windows.Forms.dll

Additional Information: Invoke or BeginInvoke cannot be called in a control until a window handle is created.

This is what I'm worried about.

In addition, if a more detailed explanation is required, I can try to publish some code and / or additional information about debugging output.

+4
source share
3 answers

try to use the event shown something like this

  public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Shown += new System.EventHandler(this.Form1_Shown); } private void Form1_Shown(object sender, EventArgs e) { } } 

hope this help

+18
source

The first event that fires after the form is fully loaded is the Shown event. use it ...


According to the MSDN sequence of events:

When starting the application

  • Control.HandleCreated
  • Control.BindingContextChanged
  • Form.load
  • Control.VisibleChanged
  • Form.Activated
  • Form.Shown

When the application closes:

  • Form.Closing
  • Form.FormClosing
  • Form.Closed
  • Form.FormClosed
  • Form.Deactivate

And as @Henk Holterman said in his answer, don't use wait in an event driven form ...

+6
source

You have the event Loaded and Shown .

Windows is an event, so never wait for something in a loop.

+4
source

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


All Articles