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 / C++ / MFC
  4. Messages between threads...

Messages between threads...

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
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.
  • E Offline
    E Offline
    eli15021979
    wrote on last edited by
    #1

    Hi, I have an application which writes data(e.g strings) to the events log. Now,I need to write an application which reads the data from the events log at the same time that the first application write the data(i.e Logger). In that application I want to create 2 threads: The first thread will read the data from the events log,put the data(EVENTLOGRECORD) in a message, and send it to the other thread. The second thread will receive the message from the first one,decode it and dispaly the data in a list box. My questions are: 1. Which is the best (and effective) way to send messages between threads? 2. In the second thread(the one which responsible for the display) - how does the thread receive those messages? Do I need to call PeekMessage() and DispatchMessage() in a loop? I heard about WaitForSingleObject() option,but I didn't understand how to use it... Can anyone point me to a good tutorial about messages between threads? I googled a little but could'nt find something usefull... With best regards, Eli

    D N 2 Replies Last reply
    0
    • E eli15021979

      Hi, I have an application which writes data(e.g strings) to the events log. Now,I need to write an application which reads the data from the events log at the same time that the first application write the data(i.e Logger). In that application I want to create 2 threads: The first thread will read the data from the events log,put the data(EVENTLOGRECORD) in a message, and send it to the other thread. The second thread will receive the message from the first one,decode it and dispaly the data in a list box. My questions are: 1. Which is the best (and effective) way to send messages between threads? 2. In the second thread(the one which responsible for the display) - how does the thread receive those messages? Do I need to call PeekMessage() and DispatchMessage() in a loop? I heard about WaitForSingleObject() option,but I didn't understand how to use it... Can anyone point me to a good tutorial about messages between threads? I googled a little but could'nt find something usefull... With best regards, Eli

      D Offline
      D Offline
      DevMentor org
      wrote on last edited by
      #2

      You will need to create an Event object and then use WaitForSingleObject on the event object to notify the read thread that it's ok to wake up and started reading. The log buffer will need to be protected using a critical section, so that the writer thread is blocked from writing to the log while the reader thread is still reading. Once the reader thread is done, it exits the critical section and then goes back to waiting on the event object.

      --- Yours Truly, The One and Only! devmentor.org Design, Code, Test, Debug

      1 Reply Last reply
      0
      • E eli15021979

        Hi, I have an application which writes data(e.g strings) to the events log. Now,I need to write an application which reads the data from the events log at the same time that the first application write the data(i.e Logger). In that application I want to create 2 threads: The first thread will read the data from the events log,put the data(EVENTLOGRECORD) in a message, and send it to the other thread. The second thread will receive the message from the first one,decode it and dispaly the data in a list box. My questions are: 1. Which is the best (and effective) way to send messages between threads? 2. In the second thread(the one which responsible for the display) - how does the thread receive those messages? Do I need to call PeekMessage() and DispatchMessage() in a loop? I heard about WaitForSingleObject() option,but I didn't understand how to use it... Can anyone point me to a good tutorial about messages between threads? I googled a little but could'nt find something usefull... With best regards, Eli

        N Offline
        N Offline
        nbugalia
        wrote on last edited by
        #3

        If the threads belong to the same process, you can make a global structure, and can share the object of the struct in both threads for communication. In this case you need not to call PeekMessage and DispatchMessage. You can also use PostThreadMessage. In this case the thread to which the message is posted retrieves the message by calling the GetMessage or PeekMessage function. When a running program encounters WaitForSingleObject(), it waits untill the given thread/object does not enters in the signaled state or the time-out interval elapses.

        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