C# 如何停止当前方法调用的执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/746548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:36:33  来源:igfitidea点击:

how to stop the execution of current method call

c#

提问by Arunachalam

if a condition occurs i have to stop the execution of the current method call and return to the state before the method call .how can i do it .. suppose a i am executing some sample method and a condition occurs and i am prompting a message box and then i want to return to state before this function call

如果出现条件,我必须停止当前方法调用的执行并返回到方法调用之前的状态。我该怎么做.. 假设 AI 正在执行一些示例方法并且条件发生并且我正在提示一个消息框然后我想回到这个函数调用之前的状态

采纳答案by JP Alioto

You can use the Memento patternto implement object rollback. From here...

您可以使用备忘录模式来实现对象回滚。从这里...

The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.

看守人将要对发起者做一些事情,但希望能够撤消更改。管理员首先向发起人索要纪念品。然后它执行它要执行的任何操作(或操作序列)。为了回滚到操作之前的状态,它将备忘录对象返回给发起者。

Once you receive the event that indicates that you should roll back, you can undo the change and return to the caller. Here is some info. and linkson why you should not use Thread.Abort.

一旦您收到指示您应该回滚的事件,您就可以撤消更改并返回给调用者。这是一些信息。以及为什么不应该使用 Thread.Abort 的链接

回答by lc.

Is it an error condition that could have been checked before calling the method? If so, throw an exception.

在调用方法之前是否可以检查错误情况?如果是,则抛出异常。

Otherwise, returnsomething meaningful (e.g. if it's a voidfunction, change it to return a bool, and return false).

否则,return一些有意义的东西(例如,如果它是一个void函数,请将其更改为 return abool和 return false)。

回答by tpdi

This is what exceptions are for. You throw an exception to terminate the function and any caller, until an exception handler is reached.

这就是例外的用途。您抛出异常以终止函数和任何调用者,直到到达异常处理程序。

Note that this should only be done if something exceptional has occurred; exceptions shouldn't be used as a "different kind of" return value, as they are more costly in terms of code size (whether thrown or not) and running time (if thrown) , than normals returns.

请注意,只有在发生异常情况时才应这样做;异常不应用作“不同类型”的返回值,因为它们在代码大小(无论是否抛出)和运行时间(如果抛出)方面比正常返回的成本更高。

As far as returning to the state you had before, this is possible ifyour code and any library code through which the call proceeded was written in an exception safemanner.

就返回到之前的状态而言,如果您的代码和调用进行的任何库代码是以异常安全的方式编写的,那么这是可能的。

回答by shahkalpesh

I guess you are talking on the lines of object transactions OR transactional memory.

我猜你是在谈论对象事务或事务内存。

The least you can do is you can record the state (assignments) of the object being modified and write the old values on it when the condition to assignment fails.

您至少可以记录正在修改的对象的状态(分配),并在分配条件失败时在其上写入旧值。

回答by Smashery

If I'm understanding you correctly, you wish to undo changes you have made to certain variables if some condition is true? If that's the case, you will want to store a copy of all your variables (or your class as a whole). Then, if your condition comes up true, you'll have to revert all those variables to their initial state before returning from your function. It would be something like this:

如果我理解正确,如果某些条件为真,您希望撤消对某些变量所做的更改吗?如果是这种情况,您将需要存储所有变量(或整个类)的副本。然后,如果您的条件为真,您必须在从函数返回之前将所有这些变量恢复到它们的初始状态。它会是这样的:

// In order to clone your variable, you may need to inherit from 
// ICloneable and implement the Clone function.
bool MyFunction(ICloneable c)
{
    // 1. Create a copy of your variable
    ICloneable clone = c.Clone();

    // 2. Do whatever you want in here
    ...

    // 3. Now check your condition
    if (condition)
    {
        // Copy all the attributes back across to c from your clone
        // (You'll have to write the ResetAttributes method yourself)
        c.ResetAttributes(clone);

        // Put a message box up
        MessageBox.Show("This failed!");

        // Now let the caller know that the function failed
        return false;
    }
    else
    {
        // Let the caller know that the function succeeded
        return true;
    }
}

回答by AnnaR

Another solution, differing slightly from those above:

另一个解决方案,与上面的略有不同:

Check for the specified condition a bit now and then in your sample method.

在您的示例方法中不时检查指定的条件。

public void MyMethod()
{
some code

if (conditionOccurred == true){ reset to the previous state and exit;}

more code
}

This is probably not according to the book, but it gives quite simple and readable code if you don't use it too often.

这可能不是书中所说的,但是如果您不经常使用它,它会提供非常简单易读的代码。

I probably don't have to mention that you need save the program's state if you want to be able to return to it, and you need to write some code that returns you to this state.

我可能不必提及,如果您希望能够返回到程序的状态,您需要保存程序的状态,并且您需要编写一些代码将您返回到该状态。

回答by waxwing

A generic rollback functionality on the heap is for me unheard of. But you can use the Command pattern to get undo functionality and use it for rolling back:

堆上的通用回滚功能对我来说是闻所未闻的。但是您可以使用命令模式来获取撤消功能并将其用于回滚:

http://en.wikipedia.org/wiki/Command_pattern

http://en.wikipedia.org/wiki/Command_pattern

Essentially you encapsulate an operation in an object that stores enough information of the change that it can undo it. You push that object onto a stack, and when your condition occurs, you go pop all command objects from the stack and undo them. Without more information about your case it's difficult to give more specific information or tell whether this is applicable for you.

本质上,您将操作封装在一个对象中,该对象存储了足够的更改信息,可以撤消更改。您将该对象推入堆栈,当条件发生时,您从堆栈中弹出所有命令对象并撤消它们。如果没有关于您的案例的更多信息,就很难提供更具体的信息或判断这是否适用于您。