0
我有一个套接字封装类,发送和接收消息与服务器(特别是命令,响应和通知)。我还有一个消费类,它将这些命令序列化为文本以通过包装器发送。我的问题是,当一个命令被发送时,我需要等到我收到一个带有特定标题的消息。当我这样做时,由于我使用由接收方法设置的ManualResetEvent.WaitOne,因此消耗类会锁定。当我的消费类锁定是另一个消息可能在此期间被接收并且我无法处理它时,这是一个问题。有没有办法锁定一个特定的对象,所以我的班级仍然可以响应其他事件?C#异步套接字通信阻塞
发送方法:
_waitingCommands.Enqueue(command);
String commandText = command.ToString();
_socket.Send(commandText);
Receive方法:
if (response.Header.Type == Messages.MessageType.Response)
{
if (command.Response == null)
command.Response = response;
_waitingCommands.Dequeue();
command.Finished.Set();
}