How do I execute code AFTER a form has loaded?
You could use the "Shown" event: MSDN - Form.Shown
"The Shown event is only raised the first time a form is displayed; subsequently minimizing, maximizing, restoring, hiding, showing, or invalidating and repainting will not raise this event."
How can I execute code after my form starts?
Processing that occurs after Application.Run
is usually triggered in the form's Load
event handler. You can easily create a Load
method in Visual Studio by double clicking any open space on the form.
This would look something like this.
private void ReconcilerConsoleWindow_Load(object sender, EventArgs e)
{
if (CallSomeMethod())
{
this.SetLogText("True");
}
}
The reason this is (as stated in several other answers) is that the main thread (the one that called Application.Run(window)
) is now taken up with operating the Message Pump for the form. You can continue running things on that thread through messaging, using the form's or forms' events. Or you can start a new thread. This could be done in the main method, before you call Application.Run(window)
, but most people would do this in Form_Load
or the form constructor, to ensure the form is set up, etc. Once Application.Run
returns, all forms are now closed.
How to execute code every time a form is shown?
There is an event called Shown that gets fired after Show is called.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.shown?view=netframework-4.8
There is another that is called when it is Activated
I think this is what you want
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.activated?view=netframework-4.8
There is also VisibleChanged on controls.
It is all documented here:
https://learn.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms?view=netframework-4.8
This includes focus and validation events for forms at the bottom of the page.
Execute code when form loaded and before Form closes
I suggest you read through the WinForms event ordering as posted on MSDN:
http://msdn.microsoft.com/en-us/library/86faxx0d.aspx
Wait until form is finished loading
try to use the shown event 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
How to run code when form is shown?
There is a Shown event for a windows form. Check out: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown.aspx
Or if you are lazy, here you go:
public Form1()
{
InitializeComponent();
Shown += Form1_Shown;
}
private void Form1_Shown(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
Text = "Done";
}
Enjoy.
Related Topics
Why Does C# Limit the Set of Types That Can Be Declared as Const
How to Draw a Rounded Rectangle as the Border for a Rounded Form
.Net Asynchronous Stream Read/Write
Differencebetween Casting and Conversion
Which Is the Correct C# Infinite Loop, for (;;) or While (True)
Why Do I Need a Memory Barrier
Best Method to Obfuscate or Secure .Net Assemblies
Why the CPU Performance Counter Kept Reporting 0% CPU Usage
What Is the Fastest Way to Convert a Float[] to a Byte[]
How to Make a Property Protected and Internal in C#
Add Shape Information to a Listview When Its Created
Checking If an Object Is Null in C#