UI Thread and FileSystemWatcher.WaitForChanged method

  • Thread starter Thread starter Matthew Wieder
  • Start date Start date
M

Matthew Wieder

The documentation specifies that the WaitForChanged method should not be
called on the UI thread or it will freeze the UI thread. So, I have
started it in a thread seperate from the main form's thread. The
WaitForChanged event will wait infinitely until a file is created, but
the user needs to be able to stop the application from running and so I
need to stop that thread waiting for the file. I have hookde up a
ManualResetEvent to the method calling WaitForChanged, but it only
checks if the thread has been stopped after the call to WaitForChanged
returns. How can I force the WaitForChanged method to come back before
a file is found?
thanks!
 
Hi Matthew,

Call the Interrupt method on the thread that is waiting for the file changes
(see class Thread, method Interrupt in your documentation). This will cause
the WaitForChanged method to throw a ThreadInterruptedException. This
effectively unblocks your call.

HTH,
Tom T.
 

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

Back
Top