jones jones - 22 days ago 8
C# Question

Add delay between function calls in main

In my main function (Form1_Load) i'd like to call 3-4 functions to initially setup the program. Problem is that im getting an error when trying to do this (one function is not running properly). I am able make a button for each function call and press them in the right order using the Windows Form and the program works fine. So i'm thinking that the program is not connecting properly to the server , execution of functions are too fast or something causing problems with threads, so I need to add some delay between the function calls. How do i do this the best way in C#? Idea:

Form1_Load(object sender, EventArgs e) {
callfunction1();
add_delay();
callfunction2()
add_Delay();
etc.
}


This is one of my first times programming in C# so im sorry if the answer is obvious.

Answer

You can use the Thread.Sleep Function to achieve this

usage: Thread.Sleep(milliseconds);

make sure to add using System.Threading; to the top of your file

Note that This will freeze your form until the sleep is over. to keep your UI active you could run another Thread or use a Timer