Author Topic: FileSystemWatcher Class  (Read 3630 times)

0 Members and 1 Guest are viewing this topic.

HD

  • Guest
FileSystemWatcher Class
« on: November 14, 2006, 12:22:21 PM »
Hello,

I have been playing around with the System.IO.FileSystemWatcher Class

The problem that I am having is that I receive a nasty FATAL ERROR: Unhandled Access... message when I try to open a drawing from a watched folder.

Can someone tell me if I am taking the wrong approach in trying to open a drawing?

Code: [Select]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Runtime;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace MyStuff.AutoCAD
{
    public class DWGWatcher
    {
        [CommandMethod("DWGWatch", CommandFlags.Session)]
        public void DWGWatch()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nDWGWatch is running");

            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Path = "C:\\TEMP\\";

            // Add event handlers.
            fsw.Created +=new FileSystemEventHandler(fsw_Created);

            // Begin watching.
            fsw.EnableRaisingEvents = true;
        }

        // Define the event handlers.
        public void fsw_Created(object sender, FileSystemEventArgs e)
        {
            MessageBox.Show(e.FullPath + ": " + e.ChangeType);
            Document doc = AcadApp.DocumentManager.Open(e.FullPath, false);
            //
            // Stuff ToDo...
            //
        }
    }
}

Regards,

Nick

Chuck Gabriel

  • Guest
Re: FileSystemWatcher Class
« Reply #1 on: November 14, 2006, 12:39:13 PM »
I think you need to make fsw a static member of the DWGWatcher class, so it doesn't go out of scope immediately when DWGWatch() exits.

Glenn R

  • Guest
Re: FileSystemWatcher Class
« Reply #2 on: November 14, 2006, 05:56:38 PM »
Hmmm....

1. If you open a dwg from your temp directory, it will create a .dwl file, which your event handler will react to and try and open in Acad...not a good scenario.

2. The fatal error is coming from the fact that if you say WBLOCK a block INTO your watched folder, it detects the file creation, reacts, BUT, Acad hasn't finished writing the file and your event is trying to open it...not good either.

Just some thoughts off the top of my head.

Cheers,
Glenn.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: FileSystemWatcher Class
« Reply #3 on: November 22, 2006, 11:35:47 AM »
One problem that I am having is that after copying a DWG into the "C:\TEMP\" watched folder, I have to mouse click on the AutoCAD application window or current AutoCAD drawing window and then hover my mouse over the current AutoCAD drawing window before a DWG will open.
I wonder if you have to make Acad the primary process?  I thought I had to do this so I found one way.  Let me see if I can find it again, and I will post it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.