I’ve been working on a thread which will live as long as the application is running, and runs at a interval of 500ms. I noted that I could be uselessly processing if there’s nothing in the queue for it to process, so I went around looking at some sources I had locally, and I found an example close to mine, but it’s in Java.
The example had this:
synchronized(this) {
try {
wait();
} catch (InterruptedException e) {
cleanup();
break;
}
}
Inside a while loop which goes on forever.
The thread has this to notify the wait:
synchronized(this) {
notifyAll();
}
This was inside the enqueue thread.
I’d also like you to note that the class inherits Runnable.
Could anyone quickly explain the corresponding functions in C#? And maybe an example if you could!
.NET/C# best practice would be to use an EventWaitHandle.
You’d have some variable shared between the threads as so:
In the consumer thread (the one that you’re waking up every 500ms right now), you’d loop waiting for the handle (perhaps with a timeout):
And in the producer thread: