locked
Stopping all execution and wait for event RRS feed

  • Question

  • Hi,

    I have looked at a lot of similar questions and have not found an answer to a seemingly simple problem. I want to stop all execution of any script if a certain condition is met.

    I have tried to use object oriented programming, so now i have a lot of objects calling each other in different modules. Thus end and exit do not work because then the script will just continue running a level higher.

    To clarify: In form1 I have a subroutine, which calls another subroutine in a different module. This subroutine calls a function in which if a conditional statement is true all calculation has to stopped and form1 should wait for a next user event.

    I do not want the program to sleep or use a do while loop to wait for a user event, i have tried these suggestions from other questions and these haven't had the required effect.

    I would love to solve this problem without having to rewrite my entire (quite extensive) program. Any help would be greatly appreciated.

    Regards, Avi

    Friday, June 29, 2012 2:58 PM

Answers

  • Have each function report Success or Failure (at its simplest, a Boolean). Alternatively, throw an error - a bit harsh but will work. For the former, assuming proper understanding:

    Module Module1
    
        Sub Main()
            If FSub1 = False Then Return
            If FSub2 = False Then Return
            ' SUCCESS!
        End Sub
    
        Friend Function FSub1() As Boolean
            If MSub1 = False Then Return False
            If MSub2 = False Then Return False
            Return True
        End Function
    
        Friend Function FSub2() As Boolean
            If MSub1 = False Then Return False
            If MSub2 = False Then Return False
            Return True
        End Function
    
        Friend Function MSub2() As Boolean
            If Func1() = False Then Return False
            If Func2() = False Then Return False
            Return True
        End Function
    
        Friend Function MSub1() As Boolean
            If Func1 = False Then Return False
            If Func2 = False Then Return False
            Return True
        End Function
    
        Friend Function Func1() As Boolean
            ' Return False
            Return True
        End Function
    
        Friend Function Func2() As Boolean
            ' Return False
            Return True
        End Function
    
    End Module
    


    Stephen J Whiteley

    Friday, June 29, 2012 6:59 PM

All replies

  • "any script" ... Are you using VB script? This is a VB.Net forum. We call it source code, not script.

    Your description is a kind of vague. If you stop a calculation triggered by a user event, you just exit all called methods in reverse order, the program returns and waits for the next user event. So there is nothing you have to write additionally. But I guess it's not as simple as that, so you should let me know.


    Armin


    • Edited by Armin Zingler Friday, June 29, 2012 3:25 PM "not" missing
    Friday, June 29, 2012 3:24 PM
  •  I have also looked at:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx

    But it all seems to end with Application.Exit(). This is not neccessary in this case. All execution just needs to stop.

    Regards, Avi

    Friday, June 29, 2012 3:28 PM
  • It is indeed written in VB.net and I should have been more careful with the word script, it is just frustrating that there is a simple problem with seemingly no solution.

    Form1 contains a set of subroutines (e.g. fsub1, fsub2 etc.) calling other subroutines (e.g. msub1, msub2 etc.) in a different module. The subroutines (msub1) in the module call functions (e.g. func1 , func2 etc.) in a certain order. There is a conditional statement in func1 which can arise due to number of possibilities.

    What do I program in this conditional statement to not only stop execution of func1, but to also stop execution of all functions in msub1 and eventually all subroutines in form1 (fsub1 etc.)?

    In a normal calculation first fsub1 will execute, which triggers msub1 then func1 then func2 etc. then msub2 etc. then fsub2 etc. I want if there is the error all calculation to stop after func1. If I exit func1, func2 still executes. If I exit msub1, msub2 still executes. If I exit fsub1, fsub2 still executes. To build in an exit for the same conditional statement in each subroutine which has to exit would take hours.

    i hope this makes the problem a bit more clear.

    Friday, June 29, 2012 3:39 PM
  • There's is no other way than exitting all methods in reverse order. You must design each method so that it supplies the necesssary information to the caller. Based on this information, the caller can decide whether and how to continue or not.

    I've constructed this - pretty imaginary - example: (see also notes below)

       Sub start()
    
          Dim res As Double
    
          If Calc1(res) Then
             MsgBox(res)
          Else
             MsgBox("error")
          End If
    
       End Sub
    
       Function Calc1(ByRef p1 As Double) As Boolean
    
          Dim result As Double
    
          If Calc2(5, -2, -3, 17, result) Then
             p1 = result * 3
             Return True
          Else
             Return False
          End If
    
       End Function
    
       Function Calc2( _
          ByVal p1 As Integer, _
          ByVal p2 As Integer, _
          ByVal p3 As Integer, _
          ByVal p4 As Integer, _
          ByRef result As Double) As Boolean
    
          Dim sum = Calc3(p1, p2, p3)
    
          If sum = 0 Then
             Return False
          Else
             result = p4 / sum
    
             Return True
          End If
    
       End Function
       Function Calc3(ByVal p1 As Integer, ByVal p2 As Integer, ByVal p3 As Integer) As Integer
    
          Return p1 + p2 + p3
    
       End Function

    Start callc Calc1 calls Calc2 calls Calc3. There is a division by zero involved that would occur if the sum = 0. That's the situation you have described. In Calc2, the situation is handled by not performing the division but instead returning False. The caller Calc1 sees that Calc2 returned False and consequently also does not do any further action and returns False.



    Armin


    Friday, June 29, 2012 4:08 PM
  • Have each function report Success or Failure (at its simplest, a Boolean). Alternatively, throw an error - a bit harsh but will work. For the former, assuming proper understanding:

    Module Module1
    
        Sub Main()
            If FSub1 = False Then Return
            If FSub2 = False Then Return
            ' SUCCESS!
        End Sub
    
        Friend Function FSub1() As Boolean
            If MSub1 = False Then Return False
            If MSub2 = False Then Return False
            Return True
        End Function
    
        Friend Function FSub2() As Boolean
            If MSub1 = False Then Return False
            If MSub2 = False Then Return False
            Return True
        End Function
    
        Friend Function MSub2() As Boolean
            If Func1() = False Then Return False
            If Func2() = False Then Return False
            Return True
        End Function
    
        Friend Function MSub1() As Boolean
            If Func1 = False Then Return False
            If Func2 = False Then Return False
            Return True
        End Function
    
        Friend Function Func1() As Boolean
            ' Return False
            Return True
        End Function
    
        Friend Function Func2() As Boolean
            ' Return False
            Return True
        End Function
    
    End Module
    


    Stephen J Whiteley

    Friday, June 29, 2012 6:59 PM
  • I explored throwing an exception and that worked fine. Thanks for the suggestion!
    Monday, July 2, 2012 7:55 AM