简体   繁体   中英

How to determine that a form has finished opening in C#

I'm having some problems with my form and the business class:

The form does not open until the business class has finished it's work. Every time I call Form1 in Main() , Form1 calls two methods:

InitializeComponents();
testConnection();

testConnection calls the business class and sets the properties of the Form according to the properties of the business class. Like that:

Pingger pingger = new Pingger();
ipLabel.Text = pingger.getLocalIP();

I do not do just these operations, I also do others like the result of a ping. However, Form1 takes a long time to open and when it's open Form1 shows the results. What do I have to do, create Form1, open it, and then after 3 seconds start to process the business class?

What you want is an event raised by your form control. To determine if there is such an event you simply Google the class (ie Form ). On the msdn page every property, method and event is listed accompanied with what they do.

What you want is the Shown event. You can add an event to this and then handle whatever it is you want to do. See link below.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form(v=vs.110).aspx

You can subscribe to the Shown event for the form and call your testConnection() method from there.

InitializeComponents();
Shown += (s,e) => testConnection();

You could also use multi-threading to "launch" the testConnection() method. (but then you have the added complexity of making sure the results are in before you rely on them, thread safety, etc...)

I wonder you want to show the form just before the connection is tested?

The idea could be using multi-threading like the Wonderbird suggestion, using some like this:

    InitializeComponent();
    new Thread(testConnection).Start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM