How Do I Propagate Exception Back to Host Thread?

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?
 
A

Alan Pretre

Marc said:
How do I report an exception in my background thread back
to the host thread in case the delete operation failed?

What I have done is define and create an "Exception" event and have the
background thread raise it to the main thread. The payload for the event is
the exception object, IIRC.

-- Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top