Home All Groups Group Topic Archive Search About

End all running code of .dll

Author
9 Aug 2006 1:45 PM
JakkyTchong
Hi,

I'm writing a .dll. I'd like to know how to stop all the code running
from anywhere in the solution. For example:
-A class of the .dll is called from an .exe.
-Function A is called from the main sub.
-Function A calls Function B.
-Function B calls Function C.
-I need to terminate all the running code from Function C with a simple
method/function.

Is there an easy way to do this? Sometimes it gets very long to
propagate error handling back tho the main sub when deep into
functions.

I get this error when writing "End": 'End' statement cannot be used in
class library projects.

It seems 'End' statement doesn't work in .dlls...

Any ideas?

JakkyTchong

Author
9 Aug 2006 2:21 PM
Phill W.
JakkyTch***@yahoo.com wrote:

> -A class of the .dll is called from an .exe.
> -Function A is called from the main sub.
> -Function A calls Function B.
> -Function B calls Function C.
> -I need to terminate all the running code from Function C with a simple
> method/function.
>
> Is there an easy way to do this? Sometimes it gets very long to
> propagate error handling back tho the main sub when deep into
> functions.

And that's what Exceptions are all about.

You'll probably need a flag defined at the class level that is read by
the lowest-level function.
When it sees this flag being set, the method throws an Exception.
The Exception is caught and handled by the top-level function, which
then decides what, if anything it can do about it.

Class Z
    Private m_bOops As Boolean = False

    Sub A()
       Try
          B()
       Catch ex As Exception
          Return ' or whatever
       End Try
    End Sub

    Sub B()
       C()
    End Sub

    Sub C()
       Do While True
          If m_bOops Then
             Throw New ApplicationException( "Boom" )
          End If
       End Do
    End Sub

End Class

> It seems 'End' statement doesn't work in .dlls...

I would suggest that End is never required.

HTH,
    Phill  W.
Author
10 Aug 2006 6:45 PM
JakkyTchong
Thanks Phill for the very useful snippet of code. That would work well
I think. Do yo know if it would be possible to do approximately the
same thing using events? I don't have any experience with events at
all. Would it be simple enough to implement?

JakkyTchong

Phill W. wrote:
Show quoteHide quote
> JakkyTch***@yahoo.com wrote:
>
> > -A class of the .dll is called from an .exe.
> > -Function A is called from the main sub.
> > -Function A calls Function B.
> > -Function B calls Function C.
> > -I need to terminate all the running code from Function C with a simple
> > method/function.
> >
> > Is there an easy way to do this? Sometimes it gets very long to
> > propagate error handling back tho the main sub when deep into
> > functions.
>
> And that's what Exceptions are all about.
>
> You'll probably need a flag defined at the class level that is read by
> the lowest-level function.
> When it sees this flag being set, the method throws an Exception.
> The Exception is caught and handled by the top-level function, which
> then decides what, if anything it can do about it.
>
> Class Z
>     Private m_bOops As Boolean = False
>
>     Sub A()
>        Try
>           B()
>        Catch ex As Exception
>           Return ' or whatever
>        End Try
>     End Sub
>
>     Sub B()
>        C()
>     End Sub
>
>     Sub C()
>        Do While True
>           If m_bOops Then
>              Throw New ApplicationException( "Boom" )
>           End If
>        End Do
>     End Sub
>
> End Class
>
> > It seems 'End' statement doesn't work in .dlls...
>
> I would suggest that End is never required.
>
> HTH,
>     Phill  W.
Author
11 Aug 2006 11:54 AM
Phill W.
JakkyTch***@yahoo.com wrote:
> Do yo know if it would be possible to do approximately the
> same thing using events? I don't have any experience with events at
> all. Would it be simple enough to implement?

An event can spike a "notification" to some routine that is interested
in it, but can't affect the flow of execution - you can't get out of a
function by raising an event; you can by throwing an Exception.

HTH,
    Phill  W.
Author
11 Aug 2006 2:00 PM
JakkyTchong
Alright then! An Exception it will be!

Thanks a lot Phill for your help and have a great weekend!

JakkyTchong


Phill W. wrote:
Show quoteHide quote
> JakkyTch***@yahoo.com wrote:
> > Do yo know if it would be possible to do approximately the
> > same thing using events? I don't have any experience with events at
> > all. Would it be simple enough to implement?
>
> An event can spike a "notification" to some routine that is interested
> in it, but can't affect the flow of execution - you can't get out of a
> function by raising an event; you can by throwing an Exception.
>
> HTH,
>     Phill  W.