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

前端 未结 12 1439
我在风中等你
我在风中等你 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

    Use Microsoft's Reactive Framework (NuGet "System.Reactive") and then you can do this:

    protected void Execute(Action action, int timeout_ms)
    {
        Scheduler.Default.Schedule(TimeSpan.FromMilliseconds(timeout_ms), action);
    }
    

提交回复
热议问题