Lienau
Lienau

Reputation: 1353

Code running before form showing. c#

I have code that I run when my form runs (I tried below InitializeComponent(); and with form.load event), I need the code to run after the form is visible as it takes about 5 seconds to run so you cant see the form for about 5 seconds. How can I use my code so that it runs after the form is visible? Would I have to use a backgroundWorker? thanks.

Upvotes: 1

Views: 470

Answers (3)

Bobby
Bobby

Reputation: 11576

You're looking for the Shown-Event of the form I guess.

Upvotes: 1

ChrisF
ChrisF

Reputation: 137108

It might be an idea to spawn a thread to do the work in the constructor and then enable/set the controls on the form on the worker completed event. That way the form is visible as soon as possible and your code runs as early as possible and neither gets in each others way.

Upvotes: 1

Adam Lear
Adam Lear

Reputation: 38758

The background worker would be a good thing to spawn off from the Shown event in this case. That way you're not tying up the UI thread after displaying the form to perform your resource-intensive task. Otherwise the form will display, but remain unresponsive while your code is running.

Upvotes: 2

Related Questions