G
Guest
I'm building a service application that monitors a directory using a
FileSystemWatcher that captures "create" events. In my "create" event
handler, I do all my custom processing of the new file and then I just need
to delete the file at the end. Unfortunately, the file has a "write" lock on
it so I can't simply just use File.Delete(). (Oddly, this strange lock does
not exist if I run the program as a console application instead of as a
service, but that's not my primary question.)
To work around this weird behavior, I have opted to spawn a simple
background thread via ThreadPool.QueueUserWorkItem() so that the "create"
event handler can exit and release the lock, and then I can do my file
cleanup operations in the background thread. This seems to work, but I'm new
to .NET threading. How do I report an exception in my background thread back
to the host thread in case the delete operation failed?
FileSystemWatcher that captures "create" events. In my "create" event
handler, I do all my custom processing of the new file and then I just need
to delete the file at the end. Unfortunately, the file has a "write" lock on
it so I can't simply just use File.Delete(). (Oddly, this strange lock does
not exist if I run the program as a console application instead of as a
service, but that's not my primary question.)
To work around this weird behavior, I have opted to spawn a simple
background thread via ThreadPool.QueueUserWorkItem() so that the "create"
event handler can exit and release the lock, and then I can do my file
cleanup operations in the background thread. This seems to work, but I'm new
to .NET threading. How do I report an exception in my background thread back
to the host thread in case the delete operation failed?