scottoffen / grapevine-legacy

Embedding A REST Server In Your Application Should Be Simple
http://scottoffen.github.io/grapevine-legacy/
Apache License 2.0
209 stars 50 forks source link

Automatically shutdown server on program shutdown #182

Closed tmontney closed 7 years ago

tmontney commented 7 years ago

If you close a program's front-end, Grapevine does not shutdown automatically. This leaves whatever port you were listening on open, and it's very difficult (if not impossible) to stop it without rebooting the entire machine.

You can accomplish this automatically by shutting down the server if the object is disposed. The object is disposed when the main thread is destroyed. Something like the following needs to be added to the server's constructor.

Private Sub Server_Close(sender As Object, e As EventArgs) Handles MyBase.Disposed
    If svr IsNot Nothing AndAlso svr.IsListening Then svr.Stop()
End Sub
scottoffen commented 7 years ago

After some looking around, I am unable to find a way to do this that works. If you have a suggestion or working code, please post it here. Otherwise, I would recommend using the provided Dispose pattern.