I implemented FileSystemWatcher in a windows forms application. Changed event is working when I open a file (say pdf-1) for the first time. But when I open the same file for the second time within short span of time, changed event is not firing. But it is firing when I open another file (say pdf-2). Again the changed event is firing for the first file (pdf-1) only if I open it after some time (say 1 or 2 hours).
I set the InternalBufferSize to 16KB and the NotifyFilters used are LastAccess, LastWrite, FileName and DirectoryName.
I am unable to find out the issue. Can anyone help me out?
I have seen similar results with FileSystemWatcher for RAID controller disks, SCSI disks and regular IDE disks with write cache enabled.
I also experienced a lot of other errors that may have been caused by thread synchronization problems. Here is another description of this problem.
Related
I have two applications having reference to same xml file. One writes to the file, other monitors changes to the file.
In Application 1, I have used FileSystemWatcher to monitor changes, applied filter as LastWrite and watching for change event only. It runs continously in background
Application 2 reads a specific node in the file at startup and updates the node on users direction. Its a simple windows form, that reads a value from file when executed, and has the facility to change that value
Strange thing I came across when running both the applications, is that when application 2 starts up, file change event is raised in application 1 (Note: Application 1 continously watches the file)
I want to get rid of the change event raising in application 1 when application 2 starts up.
Is the filter incorrect or the Change event behaving nasty?
This behavior is documented:
The Changed event is raised when changes are made to the size, system
attributes, last write time, last access time, or security permissions
of a file or directory in the directory being monitored.
If Application 2 opens the file and the OS keeps track of last access times, a Changed event will be raised. Since there's no facility to filter only those events that actually write data to the file, you will have to determine what happened by keeping state on Application 1.
For example, now that you know what's going on you can keep a cache of the last write time and decide to ignore the event if that hasn't changed.
My WPF app uses Log4Net to record messages to the event viewer. This is working great on most machines. However, there are two machines in my office where there are problems. One is a physical Windows 7 machine with 2 GB of ram, the other is a virtual machine running XP, which also has 2 GB of ram.
The problem is that even though the users are logged in using accounts with administrator rights, the system won't let them create the custom event log that I set up for my application. This is causing my program to die.
I can add error handling on all of the Log calls, but my feeling on this is I shouldn't. The messages are being logged in the catch handler for another error that already occurred. Just what am I going to do with the error information if it can't be logged?
In any event, I tried to create the custom event log on the XP virtual machine yesterday and it still wasn't created. What exactly do I need to do to get the custom event log created on these two machines?
Tony
It turns out that the problem wasn't in the logging code at all. My program uses WPF for the GUI. It's start-up sequence does the minimum amount of work on the UI Thread so it can display the UI as soon as possible.
The rest of the initialization is done on a background thread. I knew that an error was occurring, but I couldn't find the custom error log in the list of logs in the Event Viewer. It turns out that my code didn't find some data in the database that it needs and was trying to report the error. This is a 2 step process which involves first recording the error to the log and then displaying a custom MessageBox dialog. I was getting a XamlParseException when the program was trying to display this dialog.
To make a long story short, the problem that was crashing the program was the XamlParseException. This was thrown because I was calling the custom MessageBox's Show method on the background thread, not on the UI thread. Because I couldn't find the custom event source in the event viewer, I couldn't find the error, so I assumed that the error was a permisions issue.
By the way, I did try to create the event log manually at one point, and yesterday I checked the registry and did find the entry for the custom event source.
There is one other machine here that is having the same problem. I'm sure it's the same exact issue. I'm adding logic to the error handling to make sure that the custom MessageBox is always called on the UI Thread so the program won't bomb like that if the same issue recurs.
We would need to see how you tried creating the event log on the XP machine...
Generally, you need to read this: http://msdn.microsoft.com/en-us/library/49dwckkz(v=vs.80).aspx
Particularly the note discussing when to create your custom event log:
"In general, create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources and you attempt to write an event with the new source, the write operation will fail. If creating the source during installation is not an option, then try to create the source well ahead of the first write operation, perhaps during your application initialization. If you choose this approach, be sure your initialization code is running with administrator rights on the computer. These rights are required for creating new event sources."
Try creating the custom log in advance of the first logging event to use it.
I would like to detect during my C# application run-time a manual change of the system time and date and automatic daylight savings time change.
Moreover I am looking for a way to detect these changes that happened during the time the application was offline when the user launched the application.
In this page http://support.microsoft.com/kb/815314 there is an explanation how to scan the events log but I couldn't find something that will identify an event uniquely as time change event.
You can use the SystemEventsClass to respond to a time change event during runtime.
As for the event log you can try digging some info about Event ID 520 in the windows security log, this post has some info about it.
I think a better solution would be to save the current date/time/timezone/dst settings the first time your app starts. Then check them on every subsequent app restart. In conjunction with the SystemEvents class to catch runtime changed this would be infinitely better than trying to parse eventlog entries - which can be purged / cleared.
I have created an application, where it watches a folder(directory) for any *.* files. The application works when I am debugging through the VS2010 with break points.
The same application doesn't work when I remove the break point.
anything I am missing . . .
This is a common issue with FSW. A debugger helps because it slows down time. When a process generates, say, a Change event, you typically cannot open the file right away. The process still has the file open and has it locked. The debugger helps because your breakpoint gives the process time to close the file.
You'll have to slow down time in your code. Avoid spinning in the event handler, that will make you lose change notifications. The best way to do it is by putting the path of the file in a thread-safe List<> that you try to empty with a timer. This also helps you deal with multiple change notifications. And UI threading problems, FSW generates its events on a threadpool thread from which you can't update a UI (but see SynchronizingObject).
We are in the process of setting up surround scm as our source control program. We created a trigger which will run when changing the state of a file/repository. When we run it on many files the server gets several werfault.exe processes in the process list. I realize its windows error reporting, however, there is no popup. I'm trying to determine the cause of the error... is there a specific log I can check, or a debugging technique I can use? I don't believe it will be possible to debug directly on the server it runs on.
Thanks
WER faults end up in the Event Log, so that might give you a tiny clue, though it's usually not enough info to tell what went wrong. Maybe you want to add more logging to your trigger application, or have it run userdump.exe when it's about to crash (see AppDomain.UnhandledException event) to a specific folder - that way you can open up the crash dumps later to figure out what went pear-shaped.
What was happening was that we set a trigger on certain events and the trigger calls the handling exe simultaneously, which was swamping the server. We're now using an alternate solution with a windows service so we can control the max amount of simultaneous trigger executions.