Best way to create a “run-once” time delayed function in C#

前端 未结 12 1459
我在风中等你
我在风中等你 2020-12-13 08:54

I am trying to create a function that takes in an Action and a Timeout, and executes the Action after the Timeout. The function is to be non-blocking. The function must be

12条回答
  •  一个人的身影
    2020-12-13 09:12

    I don't know which version of C# you are using. But I think you could accomplish this by using the Task library. It would then look something like that.

    public class PauseAndExecuter
    {
        public async Task Execute(Action action, int timeoutInMilliseconds)
        {
            await Task.Delay(timeoutInMilliseconds);
            action();
        }
    }
    

提交回复
热议问题