How to estimate method execution time?

前端 未结 3 1377
夕颜
夕颜 2021-02-10 15:31

I have requirement to cancel method execution if it takes the more than two seconds to complete and restart it on another thread.

So, is there any way/call back mechanis

3条回答
  •  暖寄归人
    2021-02-10 16:14

    check if network drive exists with timeout in c#
    https://web.archive.org/web/20140222210133/http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time

    Async Pattern:

    public static T SafeLimex(Func F, int Timeout, out bool Completed)   
       {
           var iar = F.BeginInvoke(null, new object());
           if (iar.AsyncWaitHandle.WaitOne(Timeout))
           {
               Completed = true;
               return F.EndInvoke(iar);
           }
             F.EndInvoke(iar); //not calling EndInvoke will result in a memory leak
             Completed = false;
           return default(T);
       } 
    

提交回复
热议问题