Search code examples
c#delay

Setting a delay between methods in c#


I'm trying to run four methods, but between each time a method runs, I want the program to wait a second. Here's the code, I really have no idea how to go about this, thank you in advance!

private void go_Click(object sender, EventArgs e)
{

    {
        while (GlobalVar.Direction == "down")
        { movedown();}
        while (GlobalVar.Direction == "up")
        {moveup();}
        while (GlobalVar.Direction == "right")
        {moveright();}
        while (GlobalVar.Direction == "left")
        {moveleft();}
    }
}

Solution

  • Make your method async and use await Task.Delay(1000)

    Like this:

    private async void go_Click(object sender, EventArgs e)
    {
    
        {
            while (GlobalVar.Direction == "down")
            {  
               await Task.Delay(1000); 
               movedown();
            }
            ...
        }
    }