Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VB.NET
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 14-Sep-12 8:40am
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
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,
—SA
  Permalink  
v3
Comments
bharathimani93 at 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 at 6-Dec-13 1:49am
   
It could be your own exception class or System.Threading.ThreadAbortException designed for a related purpose.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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()
 
you can read about it here:
http://stackoverflow.com/questions/800767/how-to-kill-background-worker-completely[^]
 

Valery.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thanks!!
 
This is where I was looking for!
  Permalink  
Comments
Matt T Heffron at 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
0 Sergey Alexandrovich Kryukov 355
1 _Amy 235
2 Peter Leow 185
3 Andreas Gieriet 180
4 Dave Kreskowiak 155
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,462
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
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