super simple question, but I just wanted to clarify. I want to restart the stream using AutoResetEvent, so I call the following sequence of methods in my AutoResetEvent.
setupEvent.Reset(); setupEvent.Set();
I know this is really obvious, but MSDN does not state in its documentation that the Reset method restarts the stream, it just sets the event state without signaling.
UPDATE:
Yes, another thread is waiting in WaitOne (), I assume that when it is called, it will resume at the exact point where it stopped, which I donβt want, I want it to restart from the very beginning, The next example from this valuable The resource illustrates this:
static void Main() { new Thread (Work).Start(); _ready.WaitOne(); // First wait until worker is ready lock (_locker) _message = "ooo"; _go.Set(); // Tell worker to go _ready.WaitOne(); lock (_locker) _message = "ahhh"; // Give the worker another message _go.Set(); _ready.WaitOne(); lock (_locker) _message = null; // Signal the worker to exit _go.Set(); } static void Work() { while (true) { _ready.Set(); // Indicate that we're ready _go.WaitOne(); // Wait to be kicked off... lock (_locker) { if (_message == null) return; // Gracefully exit Console.WriteLine (_message); } } }
If I understand this example correctly, pay attention to how the main thread resumes where it stopped when the worker thread signals this, but in my case, I would like the main thread to restart from the beginning.
UPDATE 2:
@Jaroslav Jandek - It's pretty complicated, but basically I have a CopyDetection thread that launches FileSystemWatcher to control the folder for any new files that are moved or copied to it. My second thread is responsible for replicating the structure of this particular folder to another folder. Therefore, my CopyDetection thread should block this thread while the copy / move operation is in progress. When the operation completes, the CopyDetection stream restarts the second stream so that it can duplicate the folder structure with recently added files.
UPDATE 3:
@ SwDevMan81 - I really didnβt think about it, and that would work for one caveat. In my program, the original folder that is duplicated is cleared after the duplication process is complete. Therefore, I have to block and restart the second thread when new items are added to the original folder, so it may be possible to correctly analyze the new folder structure.
To solve this problem, I think it is possible to add a flag that signals that it is safe to delete the contents of the original folder. I think I could put the delete operation in my own cleanup thread.
@Jaroslav Jandek - My apologies, I thought it would be easy to restart the stream on a whim. To answer your questions, I do not delete the original folder, only its contents, this is a requirement of my employer, unfortunately, I can not change. Files in the source folder are moved, but not all of them, only files that are properly scanned by another process, the rest must be cleaned, that is, the source folder is empty. In addition, the reason for the replication of the source folder structure is that some files are contained in a very strict hierarchy of subfolders that must be stored in the destination directory. Again it is a pity that this has become complicated. All of these mechanisms in place, have been tested and work, so I did not feel the need to develop them. I only need to detect when new files are added so that I can stop other processes normally during the copy / move operation, then I can safely replicate the structure of the source folder and resume processing.