Click here to Skip to main content
12,760,377 members (33,076 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET

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.

Posted 14-Sep-12 9:40am
Sergey Alexandrovich Kryukov 14-Sep-12 15:56pm
Even though the question is kind of naive, it touches important problems, and you reasonably identified one of them, which shows good thinking. I voted 4 (and believe me, this is a rare case, as 99%+ questions these days do not deserve more than 1 or worse (removed, reported for abuse, etc.))
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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,
bharathimani93 6-Dec-13 1:22am
Hi,I am new to have the same problem you are disucssing here.I have 2 dlls. From the first dll I am calling the second dll class.The problem is the 2 dll has a set of functions to work out. If any function in the middle get exception means(some serious kind of exception i am speaking about) I have to quit the whole function and return back to the first dll.
How can I archive this without using thread ? In the means I have got little bit clear with your explanation but i am very happy if you explain it in detail.
"Exit whole callstack" simply means: throw a special exception. And catch is where you want to land".
Here what is the special exception and how can I get it.Please help me.
It could be your own exception class or System.Threading.ThreadAbortException designed for a related purpose.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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
        Catch generatedExceptionName As ThreadAbortException
            e.Cancel = True
        End Try
    End Sub

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

to abort the worker:


you can read about it here:[^]

Sergey Alexandrovich Kryukov 14-Sep-12 15:53pm
It's a close, but the question does not imply using a separate thread for anything. Of course, a thread could be used, but with equal success, it could be not. They are simply totally unrelated to the problem. The Abort it right thing, but it absolutely does not require creation of a thread in any of three ways (why BackgroundWorker only? there are two more general ways...).
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


This is where I was looking for!
Matt T Heffron 14-Sep-12 17:25pm
You should "accept" Sergey's solution and probably up-vote his solution as well.
Not that he really needs any more Reputation Points! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100