PDA

View Full Version : File Watcher Fatal Error



jason907238
2007-08-27, 02:24 PM
I created a file watcher. It works great as long as I don't try to do anything else in AutoCAD. It gives me FATAL ERROR: Unhandled Access Violation Reading 0x000 Exception @ 6e9c038h.

Here is what I did.

In my Class, I started it with

<CommandMethod("Watch")> _
Public Sub Watch()
Try
WriteLine("Starting Watcher")
StartFileWatcher()
Catch ex As Exception
End Try
End Sub
In My Module, I put...
Const DirectoryName As String = "c:\temp"
Public myWatcher As FileSystemWatcher
Public Sub StartFileWatcher()
' Create the FileSystemWatcher
myWatcher = New FileSystemWatcher(DirectoryName)
Try
' Add delegates for the desired events
AddHandler myWatcher.Created, New FileSystemEventHandler( _
AddressOf myWatcher_Created)
AddHandler myWatcher.Changed, New FileSystemEventHandler( _
AddressOf myWatcher_Changed)
AddHandler myWatcher.Renamed, New RenamedEventHandler( _
AddressOf myWatcher_Renamed)
AddHandler myWatcher.Deleted, New FileSystemEventHandler( _
AddressOf myWatcher_Deleted)
' You must set EnableRaisingEvents to true in order for events to be received
myWatcher.EnableRaisingEvents = True
' Display instructions and wait for user to exit program
WriteLine("Make changes to the {0} directory" & DirectoryName)
WriteLine("You will see events generated from those changes.")
WriteLine("Enter WatchOff to exit the program.")
Finally
End Try
End Sub

Public Sub EndFileWatcher()
Try
myWatcher.Dispose()
Catch
End Try
End Sub

' Event handlers respond to events raised by FileSystemEventHandler
Private Sub myWatcher_Changed(ByVal sender As Object, _
ByVal e As FileSystemEventArgs)
WriteLine("ChangedFile")
End Sub

Private Sub myWatcher_Created(ByVal sender As Object, _
ByVal e As FileSystemEventArgs)
WriteLine("Created File")
End Sub

Private Sub myWatcher_Renamed(ByVal sender As Object, _
ByVal e As RenamedEventArgs)
WriteLine("Renamed File")
End Sub

Private Sub myWatcher_Deleted(ByVal sender As Object, _
ByVal e As FileSystemEventArgs)
WriteLine("Deleted File")
End Sub

Public Sub WriteLine(ByVal Text As String)
'Try
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbNewLine)
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Text)
Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen()
'Catch
'End Try
End Sub

When I catch the error in WriteLine, it does nothing at all after I have started the watch.

TIA,
Jason

jason907238
2007-08-27, 03:06 PM
The actual error is...
"A first chance exception of type 'System.NullReferenceException' occurred in TestAcadClass.dll"
As I understand it measn that an exception has been thrown somewhere, not necessarily in your code. Exceptions being thrown is quite legitimate. If the exception is caught and appropriate steps taken then that's the end of it. If the exception is not caught, THEN you have an issue. This situation would crash your app in Release so the debugger will display the unhandled exception dialogue.

My problem is that I don't know what is going on, therefore I can't handle the exception.
I have tied to Dispose of myWatcher. I have also tried to run StartFileWatcher for the second time and both cause errors in the WriteLine sub. This is not limited to WriteLine. I have tired to draw a simple line and still get the same error.

jason907238
2007-08-27, 04:37 PM
I believe that it has something to do with accessing AutoCad via my module sub routine.
I can enter CommandMethod commands while the watch is still active and they will work properly. The error comes from the sub routines with in my module (like myWatcher_Created). They can open a message box but can't access AutoCad. This is why my simple WriteLine sub will not work.

Any suggestions on what I should do????

jason907238
2007-08-28, 07:19 PM
I have found that this error also occurs when someone is running a loop and has transactions within the loop. They could fix it by taking the transactions out of the loop and placing them at the end.

This might have something to do with my problem because my file event reactor is running while trying to access AutoCAD. I have tried to clear the reactor before going into the sub routine but this does not help.

Jason

jason907238
2007-08-28, 10:11 PM
I have read that when you use a file watcher with a form, you have to SynchronizingObject to the form (.SynchronizingObject = Me). I was wondering if this is my problem. If so, how could synchronize it to AutoCAD?

Jason