Auto start code on open then close after code is done running

DPavelka

Member
Joined
Mar 1, 2008
Messages
6
Programming Experience
10+
I have a program that runs some code when i click a button on a form when done running the application ends. I want to automaticaly run the code when the users clicks on the exe. I have put the code that i want to run in the startup forms load event but that doesn't start. Can anyone tell me how to run the code at start?
 
You can't put code in a form's Load event handler that is going to run for a long time. The code in the Load event handler must finish executing BEFORE the form is displayed, so if your code executes for a long time then you're not going to see the form for a long time. If you have long-running code that you want executed in a WinForms app then you need to do it in a secondary thread or your UI simply won't work. The main thread can't do your work and look after the UI at the same time. The easiest way would be to use a BackgroundWorker.
 
That is good to know. I solved the problem by starting a timer in the load event and the code i wanted to run was put in the timer.tick event. I would be interested in learning more about the backgroundworker you mentioned.
 
Back
Top