退出整个调用栈(退出所有父函数) [英] Exit whole callstack (Exit all parent functions)

查看:100
本文介绍了退出整个调用栈(退出所有父函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在寻找退出类的所有(父)子功能的有效方法.

我有一堂课,进行一些繁重的计算.后台工作人员调用该类,该过程将在启动时开始.在某些时候,我希望用户停止计算.单击按钮时,将设置一个布尔值以停止该过程.该类将定期检查此布尔值.

当布尔值为true时,我想终止所有sub/funcitons并调用清除函数.在过程的某个时刻,我有一个包含5个函数的调用堆栈.

是否有一种快速/高效的方式一次停止所有5个功能/子?

还是每个函数都必须返回一个布尔值来确定父函数是否也必须停止...并重复5次?

希望找到一个很好的解决方案以最小化代码.

谢谢!!

Hallo,

I''m searching for an efficient way to exit all (parent) subs/functions of a class.

I''ve a class that does some heavy duty calculations. A backgroundworker calls the class and the process will start at initiation. At some point I want the user to stop the calculations. When a button is click, a boolean is set to stop the process. The class will check this boolean at regular intervals.

When the boolean is true I want to terminate all subs/funcitons and call a cleanup function. At some point in the process I''ve a callstack of 5 functions.

Is there a fast/efficient way to stop all 5 functions/subs at once?

Or must each function return a bool to determine if the parent function must be stopt as well... and repeat this 5 times?

Hope to find a nice solution to minimize code.

Thanks!!

推荐答案

解决此类问题的方法众所周知,我…给人的印象是,您以某种方式错过了整个编程的基本技术,也许更错过了它们OOP等.但是不,我不敢相信你不知道我在说什么.您甚至知道正确的术语,并希望知道堆栈是如何工作的.因此,我只给您一个简短的答案;您可以潜在地开发出整个技术,这确实非常容易.而且坚固.所以,准备好……(鼓!):

退出整个调用栈"仅表示:抛出一个特殊异常.捕获就是您要降落的地方.

现在,如果这足够了,但以防万一:如果仍然没有收到,请回复;我会为您提供所有详细信息.

免责声明:我不会参加有关该主题的火焰战争.我的意思是,围绕与错误无关的情况使用结构异常处理机制的问题引起了很多争论.许多人认为,在如此广泛的情况下使用异常是一件坏事,对此有很多争论.我不想处理.我非常确定例外不是错误.它们仅用于处理运行时错误,但是原则上,它们可以用于处理许多其他情况,最好是那些可以称为异常"的情况.您的目标就是其中之一.

再次欢迎您提出后续问题.

祝你好运,
—SA
The solution of such problems is so well known that… I got an impression that you somehow missed a whole fundamental technology of programming, perhaps more fundamental them OOP and other things. But no, I cannot believe you don''t know what I''m talking about. You even know right terminology and hopefully know how the stack works. By that reason, I''m giving you only the short answer; and you can potentially develop the whole technique out of it, which is really, really easy. And robust. So, get ready… (drums!):

"Exit whole callstack" simply means: throw a special exception. And catch is where you want to land.

Now, if this should be quite enough, but just in case: if you still did not get it, please respond; and I''ll provide you with all the detail.

A disclaimer: I am not going to participate in the flame wars on this topic. I mean that there was a lot of flame wars around the problem of using structural exception handling mechanism for situations not related to errors. There are many people who think that using exceptions in such wider set of situations is a bad thing and had hard arguments about it. I don''t want to deal with that. I am quite sure that exceptions are not errors. They are just used for handling run-time errors, but in principle, they can be used for handling many other situations, preferably those which can be called "exceptional". Your purpose is one of them.

Again, your follow-up questions are welcome.

Good luck,
—SA


您好,

这是一种实现方法:

创建一个新的类AbortableWorker

Hello,

Here is a way to do it:

Create a new class AbortableWorker

Imports System.ComponentModel
Imports System.Threading

Public Class AbortableBackgroundWorker
    Inherits BackgroundWorker

    Private _workerThread As Thread

    Protected Overrides Sub OnDoWork(e As DoWorkEventArgs)
        _workerThread = Thread.CurrentThread
        Try
            MyBase.OnDoWork(e)
        Catch generatedExceptionName As ThreadAbortException
            e.Cancel = True
            Thread.ResetAbort()
        End Try
    End Sub

    Public Sub Abort()
        If _workerThread IsNot Nothing Then
            _workerThread.Abort()
            _workerThread = Nothing
        End If
    End Sub
End Class



终止工人:



to abort the worker:

AbortableBackgroundWorker1.Abort()
AbortableBackgroundWorker1.Dispose()



您可以在这里阅读有关信息:
http://stackoverflow.com/questions/800767/how-to-kill-background-完全完成 [ ^ ]


Valery.



you can read about it here:
http://stackoverflow.com/questions/800767/how-to-kill-background-worker-completely[^]


Valery.


谢谢!

这就是我要找的地方!
Thanks!!

This is where I was looking for!


这篇关于退出整个调用栈(退出所有父函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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