How can I perform a short delay in C# without using sleep?

后端 未结 5 1360
孤街浪徒
孤街浪徒 2020-12-08 14:31

I\'m incredibly new to programming, and I\'ve been learning well enough so far, I think, but I still can\'t get a grasp around the idea of making a delay the way I want. Wha

5条回答
  •  盖世英雄少女心
    2020-12-08 14:48

    By adding using System.Timers; to your program you can use this function:

    private static void delay(int Time_delay)
    {
       int i=0;
      //  ameTir = new System.Timers.Timer();
        _delayTimer = new System.Timers.Timer();
        _delayTimer.Interval = Time_delay;
        _delayTimer.AutoReset = false; //so that it only calls the method once
        _delayTimer.Elapsed += (s, args) => i = 1;
        _delayTimer.Start();
        while (i == 0) { };
    }
    

    Delay is a function and can be used like:

    delay(5000);
    

提交回复
热议问题