-1
  • I have a thread which is responsible for scheduled transferring of files.
  • The files transferring schedule is mentioned in an XML file, which can be changed by the user at any time.
  • I have created a FileSystemWatcher, which is responsible to keep watching the XML file and notify if there is any change in the XML file (so that we can get the new schedule value). This FileSystemWatcher notifies by setting a AutoResetEvent called _waitTillXmlChanges.

Question: Once a scheduled file transferring is done, I want to put that thread into Waiting state until one of the following two conditions is met:

(a). Either the next file transfer schedule date is reached

(b). OR the FileSystemWatcher has notified about a change in the .XML file

How can I make my file transferring thread waiting for above two conditions (two AutoResetEvent waiting) and proceed if anyone of them is Set() ?

Community
  • 1
  • 1
skm
  • 5,015
  • 8
  • 43
  • 104
  • 1
    Use [`WaitHandle.WaitAny()`](https://msdn.microsoft.com/en-us/library/tdykks7z(v=vs.110).aspx) – Matthew Watson Mar 28 '17 at 08:29
  • IMO you don't really need threads for that. Just use events from `FileSystemWatcher` and based on that call your routine with `Task<>`. File transfer you can schedule on every windows os using task scheduler. – mrogal.ski Mar 28 '17 at 08:30
  • @m.rogalski: I actually needs threads because this is only one part of my application. Actually different operations have to be performed by different threads if there is any change in the .XML file happens. – skm Mar 28 '17 at 08:32
  • @skm Oh... Sorry, I've misunderstood that :) – mrogal.ski Mar 28 '17 at 08:34

1 Answers1

3

You can use WaitHandle.WaitAny(handles), as described here: https://msdn.microsoft.com/en-us/library/tdykks7z(v=vs.110).aspx

int eventNr = WaitHandle.WaitAny(new WaitHandle[]{autoResetEvent1, autoResetEvent2});
Matthias247
  • 9,836
  • 1
  • 20
  • 29