0

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.

jones
  • 585
  • 3
  • 10
  • 30

2 Answers2

1

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

Pepernoot
  • 3,409
  • 3
  • 21
  • 46
0

You can use the Thread.Sleep(what time you want the thread to sleep for)//if you are sure that by that time the function would get executed.

if you want some thing else you can go for await in C# which would actually wait for the time till your function gets executed.

Janeesh
  • 41
  • 4