How to catch if SolidWorks was closed

Hello guys!

What would be the correct way to catch if user closed SolidWorks application.

I use the following code when starting my app.

......

If SeeIfSWRunning() <> True Then Me.Close()

......

' see if SolidWorks is running or not

    Public Function SeeIfSWRunning() As Boolean

        Dim p() As Process

        p = Process.GetProcessesByName("SLDWORKS")

        If p.Count > 0 Then

            'MessageBox.Show("Running")

            Return True

        Else

            MessageBox.Show("SolidWorks is not running on this computer. Please start it prior to use this app.", TPCMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            swApp = Nothing

            Return False

        End If

Lets say user accidentally closed application in the middle of design and then clicked button on my app to open the template for instance...

    ' opens a template

    Private Sub btn_OpenTemplate_Click(sender As Object, e As EventArgs) Handles btn_OpenTemplate.Click

        If SeeIfSWRunning() <> True Then Exit Sub

        Part = swApp.NewDocument(Template, 0, 0, 0)

    End Sub

Thats where I`m getting error....

An unhandled exception of type 'System.Runtime.InteropServices.COMException

I noticed that there are a few process by SW that starts with SLDWORKS

- this is when SW is open

- this is when SW is closed

Is there a another way to check this?

Thank you,

Alex.

SolidworksApi macros