A threading problem

  • Thread starter Thread starter nick.fletcher
  • Start date Start date
N

nick.fletcher

This concerns controlling access to methods in different classes.

The meat of it is as follows:

public class CommsControl
{
private InputBuffer ib = new InputBuffer();
private OutputBuffer ob = new OutputBuffer();

ib.StartComms();
ob.StartComms();
}

protected class InputBuffer
{
private lockObj;

public StartComms()
{
System.Threading.Timer t = new
System.Threading.Timer(this.ReceiveMessages, null, 0, 500);
}
private ReceiveMessages(object stateInfo)
{
lock(lockObj);
{
Do Some Stuff...........
}
}
}



protected class OutputBuffer
{
private lockObj;

public StartComms()
{
System.Threading.Timer t = new
System.Threading.Timer(this.ReceiveMessages, null, 0, 200);
}
private SendMessages(object stateInfo)
{
lock(lockObj);
{
Do Some Stuff...........
}
}
}


Now, I want to be able to block the sending of messages if the receive
message thread is still running. I can do this with a flag but thats
not very nice - Ive also tried using waitOne but with no joy. Anyone
got any suggestions? Many thanks
 
[...]
Now, I want to be able to block the sending of messages if the receive
message thread is still running. I can do this with a flag but thats
not very nice - Ive also tried using waitOne but with no joy. Anyone
got any suggestions? Many thanks

The code you posted uses two different objects for locking. So, neither
method is synchronized with the other.

If you want to synchronize the two methods, so that they cannot occur
simultaneously, they need to use a common synchronization object. Whether
this is a mutex, or a lock on a shared object, or even using a waitable
event to synchronize their efforts, the synchronization has to be through a
single object.

The question you posted is confusing for a couple of reasons:

First, in the code you posted you don't seem to be calling
OutputBuffer.SendMessages at all. But you also haven't defined
OutputBuffer.ReceiveMessages, which is what you do appear to call. So, is
it safe to assume that you actually create the OutputBuffer timer using
SendMessages? If not, why not and what is the correct code?

Second, it's not clear to me what "thread" it is you want to depend on. You
haven't created any threads. You've just created a timer that calls a
particular method on a regular basis. That happens on a thread, but there
is no "receive message thread" per se.

But hopefully, the real issue is simply that you aren't using the same
object for the send and receive as far as locking goes. If that didn't
answer your question, you might want to clarify the confusing parts of your
post.

Pete
 
Sorry

1) The missing call to send messages is a typo
2) I dont really care which thread the send and receive happens on, so
long as they dont block. Therefore Im just using worker threads
However - your assumptions were correct and hopefully everything is now
sorted.

Cheers
Nick

[...]
Now, I want to be able to block the sending of messages if the receive
message thread is still running. I can do this with a flag but thats
not very nice - Ive also tried using waitOne but with no joy. Anyone
got any suggestions? Many thanksThe code you posted uses two different objects for locking. So, neither
method is synchronized with the other.

If you want to synchronize the two methods, so that they cannot occur
simultaneously, they need to use a common synchronization object. Whether
this is a mutex, or a lock on a shared object, or even using a waitable
event to synchronize their efforts, the synchronization has to be through a
single object.

The question you posted is confusing for a couple of reasons:

First, in the code you posted you don't seem to be calling
OutputBuffer.SendMessages at all. But you also haven't defined
OutputBuffer.ReceiveMessages, which is what you do appear to call. So, is
it safe to assume that you actually create the OutputBuffer timer using
SendMessages? If not, why not and what is the correct code?

Second, it's not clear to me what "thread" it is you want to depend on. You
haven't created any threads. You've just created a timer that calls a
particular method on a regular basis. That happens on a thread, but there
is no "receive message thread" per se.

But hopefully, the real issue is simply that you aren't using the same
object for the send and receive as far as locking goes. If that didn't
answer your question, you might want to clarify the confusing parts of your
post.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top