Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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!!
Posted
Comments
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.))
--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
 
Share this answer
 
v3
Comments
bharathimani93 6-Dec-13 1:22am    
Hi,I am new to vb.net.I 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.
Sergey Alexandrovich Kryukov 6-Dec-13 1:49am    
It could be your own exception class or System.Threading.ThreadAbortException designed for a related purpose.
—SA
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:

VB
AbortableBackgroundWorker1.Abort()
AbortableBackgroundWorker1.Dispose()


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


Valery.
 
Share this answer
 
Comments
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...).
--SA
Thanks!!

This is where I was looking for!
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900