Hi All,
I'm trying to write a stand alone application that monitors the state of autocad. We are running autocad on a server to generate pdf/dxf/dwg files fully automatically. The jobs for autocad on this batchserver are generated automatically from our PLM system and picked up automatically by an addin in autocad. The problem however is keeping autocad alive without user interference. The biggest problem I'm facing is to kill AutoCAD when it runs into a Fatal Error
. If autocad now runs into a fatal error the whole process of creating pdf/dxf or dwg files is now stopped until someone manually closes autocad and restart it.
So I hope someone here can help me how to "catch" a fatal error in autocad.
Thanks alot!
Joris
This is what I've got so far...:
Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Interop
Public Class Form1
Private Property acApp As AcadApplication = Nothing
Private Property acDoc As AcadDocument = Nothing
Private Const progID As String = "AutoCAD.Application.18"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.lblText1.Text = "Checking for AutoCAD Dialogs"
Me.btnStart.Text = "Start"
Me.btnStart.Enabled = True
Me.Update()
End Sub
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
Select Case Me.btnStart.Text
Case "Start"
Me.btnStart.Text = "Stop"
acApp = Nothing
acDoc = Nothing
bgw.RunWorkerAsync()
Case "Stop"
Me.btnStart.Text = "Start"
bgw.CancelAsync()
End Select
End Sub
Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
Do
If (bgw.CancellationPending) Then
e.Cancel = True
Exit Do
End If
Try
acApp = CType(Marshal.GetActiveObject(progID), AcadApplication)
acDoc = acApp.ActiveDocument
'catch dialogs that require user input and close autocad
AddHandler acApp.BeginModal, AddressOf CloseAutoCAD
'catch a fatal error and close autocad
'...?
'...?
'...?
Catch ex As System.Exception
acApp = Nothing
acDoc = Nothing
End Try
Do While acDoc IsNot Nothing
If (bgw.CancellationPending) Then
e.Cancel = True
RemoveHandler acApp.BeginModal, AddressOf CloseAutoCAD
acApp = Nothing
acDoc = Nothing
Exit Do
End If
If Process.GetProcessesByName("acad").Count = 0 Then
Exit Do
End If
Threading.Thread.Sleep(500)
Loop
Threading.Thread.Sleep(500)
Loop
End Sub
Public Sub CloseAutoCAD()
Dim prs = Process.GetProcessesByName("acad")
For Each pr As Process In prs
pr.Kill()
Next
Do
Loop Until Process.GetProcessesByName("acad").Count = 0
End Sub
End Class