开发者

File watcher is not working without breakpoints

开发者 https://www.devze.com 2023-02-20 07:33 出处:网络
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.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消