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

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

    Why not simply invoke your action parameter itself in an asynchronous action?

    Action timeoutMethod = () =>
      {
           Thread.Sleep(timeout_ms);
           action();
      };
    
    timeoutMethod.BeginInvoke();
    

提交回复
热议问题