COM problem when using System.IO.FileSystemWatcher

dvdg wrote on 1/7/2011, 2:02 AM
Hi people

I have a big crash with vegas when trying to use the System.IO.FileSystemWatcher in order to monitor a folder and have vegas automatically import new files in the project as those become available in the folder...

I've made a standalone application for testing the fileSystemWatcher and it works ok (triggering events when files are changing in the folder)

Then I've included that in an application extension, and included some vegas call in the function triggered by the event. Even the most basic call (Vegas.DebugOut) is having Vegas to crash immediately when the event is raised.

If I'm not using any Vegas Call in the function, everything works ok when the event is raised... but it has less interest for me.

So when vegas crash, i have a popup with the following text in it:


Non UI Thread - Unhandled Exception System.InvalidCastException: Unable to cast COM object of type 'System.__Com0bject' to interface type 'Sony.Vegas.IMediaCOM'.

Unable to cast COM object of type 'System._ComObject' to interface type 'Sony.Vegas.IMediaCOM'. This operation failed because the QueryInterface call on the COM component for the interface with IID
'{5A35F16D-DCD7-472B-A171-B7C4CB081C30}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).



If someone as any ideas about that...

So just to be clear: it's possible from vegas to init a filesystemwatcher on a particular path, but when the event is triggered by a file change, if the event handler include some vegas call, vegas crash immediately.


Comments

altarvic wrote on 1/7/2011, 3:44 AM
The most important restriction of using scripting API is you cannot directly manipulate the Vegas objects on thread other than the main GUI thread. Otherwise you'll get an exception.
dvdg wrote on 1/7/2011, 5:55 AM
Thanks for this good advice.

I've managed to have the filesystemwatcher to work in the same thread than vegas by making a DockableControl and passing it to the property "SynchronizingObject" of the FileSystemWatcher like that:

DockableControl dockView = new DockableControl("File Watcher");
vegas.LoadDockView(dockView);
FileSystemWatcher fsw = new FileSystemWatcher(folderPath);
fsw.SynchronizingObject = dockView;
fsw.EnableRaisingEvents = true;


So far it seems to work. Of course i've also setup the file system watcher to react to file change with a particular filter, and now i get a nice list of file change with Vegas.DebugOut.

I hope i will not have any trouble then. I'll let you know !

The only "annoying" thing is that i get a empty windows that pops when i init the watcher, this little windows just exists to get the path to Vegas main thread.

I don't know if there is other way to get vegas main ui thread "form" object to be attached to the filesystemwatcher...

Thanks for your help !
dvdg wrote on 1/7/2011, 6:59 AM
A little additional trick:

As i don't know a way to connect my filesystemwatcher to the main ui thread without passing through a DockView, this DockView needs to remain open all the time. If closed, vegas crashes the next time filesystemwatcher tries to trigger an event.

So i've just added an event handler on the Closing Event of this dockview, in which i'm just canceling the closing event. I don't know if there is a better way to do, but this disallow the window to be closed by user.