Time limiting a method in C#

后端 未结 6 2017
小蘑菇
小蘑菇 2021-02-02 17:16

I have a Game framework where there is a list of Bots who implement IBotInterface. These bots are custom made by user with the only limitation that they must implement the inter

6条回答
  •  别跟我提以往
    2021-02-02 17:46

    Unfortunately there is no 100% safe way to terminate a thread cleanly, as you seem to want.

    There are many ways you can try to do it though, but they all have some sideeffects and downsides that you might want to consider.

    The only clean, safe, and approved, way to do this is to get the cooperation of the thread in question and ask it nicely. However, this is only a 100% guaranteed way if you're in control of the code. Since you're not, it won't be.

    Here's the problem.

    • If you do a Thread.Abort, you risk leaving the appdomain in an unsafe state. There could be files left open, network or database connections left open, kernel objects left in an invalid state, etc.
    • Even if you place the thread into its own appdomain, and tear down the appdomain after aborting the thread, you can't be 100% safe that your process won't have problems in the future because of it.

    Let's look at why cooperation isn't going to be 100% either.

    Let's say the thread in question often needs to call into your library code, in order to draw on screen, or whatnot. You could easily place a check into those methods, and throw an exception.

    However, that exception could be caught, and swallowed.

    Or the code in question could go into an infinite loop that doesn't call your library code at all, which leaves you back to square one, how to cleanly kill the thread without its cooperation.

    Which I already said you can't.

    There is, however, one method that might work. You could spawn the bot into its own process, then kill the process when it times out. This would give you a higher chance of success because at least the operating system will take care of all resources it manages when the process dies. You can of course have the process leave corrupted files on the system, so again, it's not 100% clean.

    Here's a blog entry by Joe Duffy that explains a lot about these problems: Managed code and asynchronous exception hardening.

提交回复
热议问题