Inter thread communication - Events

  • Thread starter Thread starter antarikshv
  • Start date Start date
A

antarikshv

Hello,
I am developing a software which communicates over the CAN network. It
has a thread to transmit and another to receive the messages. After
sending some messages the transmit thread holds till it receives a
response from the network. Also the messages trans/received have to be
displayed on the GUI in real time.

For holding the transmit thread, I am using AutoResetEvent.Wait()
When the response is received I am using AutoResetEvent.Set()

For Displaying the messages in real time i am using Virtual ListView
for updating the GUI.

Now the transmit thread sometimes does not recognise that a message is
received, even when the AutoResetEvent is Set.

I think the problem is due to processing of Display and the processing
of AutoResetEvent. I think both the messages for display and autorest
are put in the message queue of the OS, which will process them
sequentially. If the display takes more time to update then the
processing of autoresetevent will take more time.

Is there any solution to this problem?
I think i need to look ahead at two fronts
1. Either change the way i update the GUI with the messages, so that i
may have to change to something other than Virtual ListView
2. Or Find some other way to unblock the transmit thread when i
receive the message.

Please help.
Thanks in advance.
 
Hi Pete,
Your theory is hard to understand without a code example, but really  
doesn't make much sense anyway.  If only one thread is waiting on the  
AutoResetEvent, then then only way for it to become unsignaled is for that  
thread to attempt to wait on it and note that it's been signaled.  Any  
delays should be irrelevant, assuming you don't have some spurious call to  
Reset() on the AutoResetEvent instance somewhere.

The transmit thread has to wait for a timeout period, after that both
the threads are to be terminated.
So the autoresetevent actually does not wait infinitely.

Note that it's not really clear from your question why you feel you need  
the transmit thread in the first place.  If you are only transmitting in  
response to receipt of some message, why not just perform that transmit in  
the receiving thread, at the appropriate time?

Keeping the two functions seperate is required as they have two
different un-related complex objectives.

To provide a code sample which will generate the problem in question
is not possible.
Reason: There are two different things happening for the application,
which the OS is handling.
1st Thing: The OS is handling updation of Virtual
ListView
2nd Thing: The Os is handling informing the waiting
thread about the event.
The above two things are distributed across various functions, Listing
which will constitute the complete project code. For testing purpose,
I had tried with simple value types like Strings, but that didnt
produce the error. Only when i am using the CAN objects for display
update, i face the problem.

So, at present i believe we first need to theoretically solve the
problem, and then maybe get onto the code.

For added information about the code:
1. The receive thread raises the autoresetevent and also makes a
boolean variable true. This way when the transmit thread is waiting,
it can either get the boolean variable to be true or the event.
2. The boolean variable is declared as static volatile

So the question is
1. While the OS is busy updating the virtual listview, it cannot
process any other event. Is there any method force the OS to use the
thread pool for the display update, and continue catering to the
events raised.
2. Is there any other way other than a) raising an event & b) boolean
variable to communicate between the threads about the state of the
network response.
3. Or should there be some other control be used for displaying the
messages, i.e instead of using Virtual Listview, some other things
cvan be used?
 
Hello Pete,

As far as Transmit and Receive threads are to be integrated, i shall
look into the feasibility of the same.

Can we start first thinking theoretically as to what can be the
problem? As putting up the entire project code is not possible.

First:
Let us start first by understanding the different ways of inter-thread
communication
1. First way can be by Raising events
2. Second way can be by using the shared objects
Is there any other method?

Second:
Is Virtual ListView the right control to be used when there are
hundreds of items to be displayed and for each sub-item in the every
item, there is computations required? Can we otherwise form a string
containing all the values of message to be displayed, and then write
the string in some other control?
 
Back
Top