How to limit the execution time of a function in c sharp?

后端 未结 8 811
太阳男子
太阳男子 2020-12-30 03:13

I\'ve got a problem. I\'m writing a benchmark and I have a function than is either done in 2 seconds or after ~5 minutes(depending on the input data). And I would like to st

8条回答
  •  梦毁少年i
    2020-12-30 03:59

    Since C# and .net framework are not real-time environments, you can't guarantee even the 3 seconds count. Even if you were to get close to that, you would still have to call the

    if(timeSpan > TimeSpan.FromSeconds(3) then goto endindentifier; before every other call in the method.

    All this is just wrong so no, there is just no reliable way to do it from what I know.

    Although you can try this solution

    https://web.archive.org/web/20140222210133/http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time

    but I just wouldn't do such things in .net application.

提交回复
热议问题