Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
@Kmcnet , Welcome to Microsoft Q&A, based on my research, somebody has ever met the problem. the reason may be that the watcher object will be eligible for garbage collection if we put the object in the method.
You could try the following code to put your statement out of the method and use GC.KeepAlive(Object) Method to make it ineligible for garbage collection like the following code:
private static FileSystemWatcher fileSystemWatcher;
private static void MonitorDirectory(string path)
{
fileSystemWatcher = new FileSystemWatcher();
...
GC.KeepAlive(fileSystemWatcher;
}
Hope my answer could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.