How to limit the execution time of a function in c

2020-02-26 05:55发布

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...

How can I do it?

Thanks a lot!

8条回答
家丑人穷心不美
2楼-- · 2020-02-26 06:23

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

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.

查看更多
放荡不羁爱自由
3楼-- · 2020-02-26 06:23

The best way in C# to stop function in middle is the return keyword in function, but how do I know when to use the return keyword to stop the function in middle, after it lasts at least 3 seconds? The Stopwatch class from System.Diagnostics is the answer. This complicated function that lasts between 2 seconds to 5 minutes (depending on the input data) logically uses many loops, and maybe even recursion, so my solution for you is that, at the first line code of that function, create an instance of Stopwatch using System.Diagnostics with the new keyword, start it by calling the Start() function of the Stopwatch class, and in for each loop and loop, at the beginning, add the following code:

if (stopwatch.ElapsedMilliseconds >= 3000) { 
     stopwatch.Stop();
     // or 
     stopwatch.Reset(); 
     return;
 } 

(tip: you can type it with hands once, copy it Ctrl+C, and then just paste it Ctrl+V). If that function uses recursion, in order to save memory, make the Stopwatch global instance rather than creating it as local instance at first, and start it if it does not running at the beginning of the code. You can know that with the IsRunning of the Stopwatch class. After that ask if elapsed time is more than 3 seconds, and if yes (true) stop or reset the Stopwatch, and use the return keyword to stop the recursion loop, very good start in function, if your function lasts long time due mainly recursion more than loops. That it is. As you can see, it is very simple, and I tested this solution, and the results showed that it works! Try it yourself!

查看更多
唯我独甜
4楼-- · 2020-02-26 06:23

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.

查看更多
聊天终结者
5楼-- · 2020-02-26 06:26

Run this function in thread and kill it after 3 seconds or check elapsed time inside this function(I think it's loop there).

查看更多
男人必须洒脱
6楼-- · 2020-02-26 06:26

Use an OS callbacks with a hi performance counter, then kill your thread, if exists

查看更多
狗以群分
7楼-- · 2020-02-26 06:33

Well..., I had the same question, and after reading all the answers here and the referred blogs, I settled for this,

It Lets me execute any block of code with a time limit, Declare the wrapper method

    public static bool ExecuteWithTimeLimit(TimeSpan timeSpan, Action codeBlock)
    {
        try
        {
            Task task = Task.Factory.StartNew(() => codeBlock());
            task.Wait(timeSpan);
            return task.IsCompleted;
        }
        catch (AggregateException ae)
        {
            throw ae.InnerExceptions[0];
        }   
    }

And use that to wrap any block of code like this

    // code here

    bool Completed = ExecuteWithTimeLimit(TimeSpan.FromMilliseconds(1000), () =>
    {
         //
         // Write your time bounded code here
         // 
    });

    //More code
查看更多
登录 后发表回答