How can I wrap a method so that I can kill its execution if it exceeds a specified timeout?

后端 未结 7 1774
陌清茗
陌清茗 2020-11-30 08:55

I have a method that I would like to call. However, I\'m looking for a clean, simple way to kill it or force it to return if it is taking too long to execute.

I\'m

7条回答
  •  悲&欢浪女
    2020-11-30 09:36

    I can think of a not so great way to do this. If you can detect when it is taking too much time, you can have the method check for a boolean in every step. Have the program change the value of the boolean tooMuchTime to true if it is taking too much time (I can't help with this). Then use something like this:

     Method(){
     //task1
    if (tooMuchTime == true) return;
     //task2
    if (tooMuchTime == true) return;
     //task3
    if (tooMuchTime == true) return;
    //task4
    if (tooMuchTime == true) return;
    //task5
    if (tooMuchTime == true) return;
    //final task
      }
    

提交回复
热议问题