I have code like this in an .net core C#-Library. It tries to monitor a directory and process files either becuase the user Refreshes or the file changes
public class WatchedFolder
private FileSystemWatcher _fileWatcher;
public WatchedFolder(){
_fileWatcher = new FileSystemWatcher(@"C:\Myfolder", "*.txt")
{
NotifyFilter = NotifyFilters.LastWrite,
};
_fileWatcher.Changed += OnFilewatcherChangedFile;
}
private void OnFilewatcherChangedFile(object sender, FileSystemEventArgs e){
processFile(e.FullPath);
}
//May be called in reaction to FileSystemWatcher-Event or directly via User-Button-Click in the GUI
public void processFile(string sFilename){
//Create native Com-Component to process the file
PPBinExportLib.IAVBinExport ppBinExport = new PPBinExportLib.AVBinExport();
ppBinExport.process(sFilename);
Marshal.FinalReleaseComObject(ppBinExport);
}
}
When I call processFile directly from the GUI everything works fine but if called from OnFilewatcherChangedFile
I get an excepton when trying to create the com-component.
The native errorcode I get is 0x80010106
which means "Der Threadmodus kann nicht nach dem Einstellen geƤndert werden." (just have a german windows but it means something like you cannot change threadingmode).
However, I see that the Com-Component may call CoInitialize
to single-threaded. It may be wrong to do so anyway but I cannot change it easily.
Since I know in .net core the FileSystemWatcher
runs in another thread, I have a feeling this might be the problem? Using the Com-Component from the main-Thread works, from the FileSystemWatcher
-Thread doesn't.
So how to fix this?
I think I have to use FileSystemWatcher.SynchronizingObject
and the ISynchronizeInvoke
-Interface but how to do this?
Regards Micha
User contributions licensed under CC BY-SA 3.0