Messages between threads...
-
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
-
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
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
-
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
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.