如何限制在C锋利的函数的执行时间? [英] How to limit the execution time of a function in c sharp?

查看:273
本文介绍了如何限制在C锋利的函数的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我正在写的基准和我比在2秒钟或〜5分钟(取决于输入数据)之后或者完成的功能。我想停止的功能,如果它是超过3秒......

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 stop that function if it's executed for more than 3 seconds...

我该怎么办呢?

非常感谢!

推荐答案

最好的办法是你的函数可以检查它的执行时间往往不够决定停止它需要很长时间。

The best way would be that your function can check its execution time often enough to decide to stop it it takes too long.

如果不是这种情况,那么运行在独立的线程的功能。在你的主线程启动一个3秒计时器。当计时器到期,使用 Thread.Abort的()杀单独的线程(当然,除非该功能已经结束)。见示例代码和在函数的文档的使用preacuations。

If this is not the case, then run the function in a separate thread. In your main thread start a 3 seconds timer. When timer elapses, kill the separate thread using Thread.Abort() (of course unless the function is already over). See sample code and preacuations of usage in the function docs.

这篇关于如何限制在C锋利的函数的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆