by Damon
20. January 2005 06:00
In order to keep people well fed here, I'm thinking I'll try to post something interesting once a week or so. I've got enough things in my vault to fill in the gaps if I don't have time to write something new...
Today I'll share another small .Net Compact Framework item. You may be familiar with
OpenNet CF, which fills in the gaps for many of the familiar things missing in the compact framework. However, some of these gems rely on the existence of "aygshell.dll" as part of the OS image. This may leave you without a Managed solution
and without the ability to PInvoke your way to your goal.
One such handy tool is FileSystemWatcher. Supposing you need to write code to fire an event when a file is created in a certain directory.
There are a few things you'd want to think about:
- Thread safety.
- Ability to work with a UI thread safely
- Ability to detect if a large file is completely done, as opposed to just in the process of being created.
One approach might be to create a class, call it something original like FileSystemWatcher. This class can have Start() and Stop() methods. Internally, it will create a thread that runs at a specified interval and does a directory search based on a given path and given search pattern. (Maybe *.jpg) Since there is no Thread.Abort() on the compact framework the class will need to put mutexes in the right places so that it can be killed by having a _stop bit somewhere.
With that out of the way, you can detect changes. By using Control.Invoke and an EventHandler, you can safely notify UI controls that the file they are looking for has been found. What about knowing when a file is complete? This part could be OS specific. On CE.NET, the filesystem creates a 4096 byte file pointer on the filesystem as soon as a new file is created. Detecting unchanging file sizes greater than 4096 bytes with FileInfo will then tell you that a file is complete. If your program needs to respond to files being written to a filesystem by webservice, network share, FTP, or Http download, you can delay processing untill the file is done. Not pretty, but if your OS image is missing some things, what can you do but improvise (Or write native )? Maybe I should write a cleaner C++ version which could then be PInvoked? Maybe if I find myself with a great deal of spare time on my hands!
By adding some code, the file change and file delete events could be detected as well. I won't post the code for this particular item here but I can help anyone who needs this functionality in the CF.
ea475c7d-052b-4cfe-87f5-be5fe1d74d0d|1|5.0
Tags: