Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop Execution Within Class Library

Status
Not open for further replies.

gfender

IS-IT--Management
May 22, 2003
55
0
0
GB
Hi all,

I'm trying to stop my program in my class library. If I put an 'End' statement the compiler gives me an error that "the statement cannot be used within a class library." I could do System.Threading.Thread.CurrentThread.Abort() but it throws an exception so my program doesn't end properly.

Any ideas?

Thanks!
 
Could you not raise an event in your class and have that event subscribed to within your main program. The code in the event handler could then properly terminate the program.


Hope this helps.

[vampire][bat]
 
You would probably want to use application.exit and I think you need system.windows.forms for that.

Christiaan Baes
Belgium

"My new site" - Me
 
earthandfire,
I'm not sure I understand your suggestion. Can you clarify?

 
This is what I meant:

A very, very basic class:
Code:
Public Class Class1

  Public Event CloseMeDown()

  Public Sub DoSomethingAndExit()

    MessageBox.Show("This program will close when you close this dialog box")
    RaiseEvent CloseMeDown()

  End Sub

End Class

Part of the form:
Code:
  Private WithEvents cls1 As Class1
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    cls1 = New Class1
    cls1.DoSomethingAndExit()

  End Sub

  Private Sub cls1_CloseMeDown() Handles cls1.CloseMeDown

    MessageBox.Show("All over now!!")
    Application.Exit()

  End Sub


Although on testing:
Code:
Public Class Class1

  Public Sub DoSomethingAndExit()

    MessageBox.Show("This program will close when you close this dialog box")
    Application.Exit()

  End Sub

End Class

and therefore:
Code:
  Private cls1 As Class1
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    cls1 = New Class1
    cls1.DoSomethingAndExit()

  End Sub

also works.

In other words, you can close the application from within the class (without returning to the calling form first)



Hope this helps.

[vampire][bat]
 
I didn't ignore you chrissie (I wouldn't dare - tou're a genius), I just gave an example of my suggestion. [wink]

[vampire][bat]
 
Chrissie's way of doing it would be faster and easier.

E&F's way of doing it would give you a more centralized control of the shut down process. Good if you have application logging and external resources that could still be open at that point.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks for your suggestions! I tried using Application.Exit but since it is a library, visual studio gave me a syntax error. I like earthandfire's approach and understand it a lot better now, thanks for the explanation. As a quick fix, I am using Environment.Exit(0) but will most likely implement a more centralized control in the near future.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top