how to waiti for specific events that occurs
-
I’ve using a Queue to store data and I do polling in order to know if the Queue contain data ( that I understand is not the correct approach) For Example private void PopDataFromQueue() { bool isRspOK; byte [] data; while (true) { //If so then stop the thread if (m_isStopReadingQueueThread == true) break; if (m_queue.Count > 0) { lock (m_queue) { if (m_queue.Count > 0) { data = (byte [])m_queue.Dequeue(); isRspOK = m_scenario.Parse(sockBuf.SocketData); if (isRspOK == true) { ; } } } } } I need to implement approach that wait for event occur and not always check the Queue status The function PopDataFromQueue is define as a thread Something like this private void PopDataFromQueue() { //create timer event that indicate in case the event that I waiting for not reach after some time , need to handle it Timer timer = new timer(1000); while (true) { //If so then stop the thread if (m_isStopReadingQueueThread == true) break; Event = WaitForEvent(Queue.Add,Timer. expire); // need to wait for two type of events Switch(Event) { case Queue.Dataexist: data = (byte [])m_queue.Dequeue(); isRspOK = m_scenario.Parse(sockBuf.SocketData); if (isRspOK == true) { ; } Break; Case timer.expire: isStopReadingQueueThread =true; break; } } } I try to look in the net and I didn’t
-
I’ve using a Queue to store data and I do polling in order to know if the Queue contain data ( that I understand is not the correct approach) For Example private void PopDataFromQueue() { bool isRspOK; byte [] data; while (true) { //If so then stop the thread if (m_isStopReadingQueueThread == true) break; if (m_queue.Count > 0) { lock (m_queue) { if (m_queue.Count > 0) { data = (byte [])m_queue.Dequeue(); isRspOK = m_scenario.Parse(sockBuf.SocketData); if (isRspOK == true) { ; } } } } } I need to implement approach that wait for event occur and not always check the Queue status The function PopDataFromQueue is define as a thread Something like this private void PopDataFromQueue() { //create timer event that indicate in case the event that I waiting for not reach after some time , need to handle it Timer timer = new timer(1000); while (true) { //If so then stop the thread if (m_isStopReadingQueueThread == true) break; Event = WaitForEvent(Queue.Add,Timer. expire); // need to wait for two type of events Switch(Event) { case Queue.Dataexist: data = (byte [])m_queue.Dequeue(); isRspOK = m_scenario.Parse(sockBuf.SocketData); if (isRspOK == true) { ; } Break; Case timer.expire: isStopReadingQueueThread =true; break; } } } I try to look in the net and I didn’t
-
I’ve using a Queue to store data and I do polling in order to know if the Queue contain data ( that I understand is not the correct approach) For Example private void PopDataFromQueue() { bool isRspOK; byte [] data; while (true) { //If so then stop the thread if (m_isStopReadingQueueThread == true) break; if (m_queue.Count > 0) { lock (m_queue) { if (m_queue.Count > 0) { data = (byte [])m_queue.Dequeue(); isRspOK = m_scenario.Parse(sockBuf.SocketData); if (isRspOK == true) { ; } } } } } I need to implement approach that wait for event occur and not always check the Queue status The function PopDataFromQueue is define as a thread Something like this private void PopDataFromQueue() { //create timer event that indicate in case the event that I waiting for not reach after some time , need to handle it Timer timer = new timer(1000); while (true) { //If so then stop the thread if (m_isStopReadingQueueThread == true) break; Event = WaitForEvent(Queue.Add,Timer. expire); // need to wait for two type of events Switch(Event) { case Queue.Dataexist: data = (byte [])m_queue.Dequeue(); isRspOK = m_scenario.Parse(sockBuf.SocketData); if (isRspOK == true) { ; } Break; Case timer.expire: isStopReadingQueueThread =true; break; } } } I try to look in the net and I didn’t
you are correct by saying that the worker thread should be event-driven and should not poll... I have used the following example to learn from: Bounded Blocking Queue (One Lock)[^] and within your worker thread (it must be a thread that is started in the constructor) use:
while (true) { lock (syncRoot) { //block the thread until there is a message in the queue while (iCount <= 0) Monitor.Wait(syncRoot); //there is work to do, lets dequeue and process it class\_Message MessageWorker = messageQueue.Dequeue(); iCount--;
.
.
.this has worked out really well and eventhough it's a blocking queue it works nice and fast !