Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to waiti for specific events that occurs

how to waiti for specific events that occurs

Scheduled Pinned Locked Moved C#
tutorialdata-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ronenb
    wrote on last edited by
    #1

    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

    F A 2 Replies Last reply
    0
    • R Ronenb

      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

      F Offline
      F Offline
      freakyit
      wrote on last edited by
      #2

      you need to write your own queue that raises an event if data pushed or poped in/from queue.

      1 Reply Last reply
      0
      • R Ronenb

        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

        A Offline
        A Offline
        abiemann
        wrote on last edited by
        #3

        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 !

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups