我有一个与TCP通信一起使用的硬件组件。控制它的类有3个作业:触发设备,收听传入消息并在收到消息时引发event
:
public class Hardware
{
public event Action<string> OnHardwareMessage;
private NetworkStream stream = new NetworkStream();
public Hardware()
{
Task.Factory.StartNew(() => { Listen(); });
}
private void Listen()
{
//listen to TCP port and raise an event when a message is received
byte[] bytes = new byte[1024];
int bytesRead = stream.Read(bytes, 0, bytes.Length);
string response = Encoding.ASCII.GetString(bytes, 0, bytesRead);
if (OnHardwareMessage != null)
OnHardwareMessage(response);
}
public void Trigger()
{
//Trigger the hardware component
//the response usually takes up to 5 seconds to arrive
}
}
此类用于view-model
:
public class MainViewModel
{
private static EventWaitHandle hardwareWaiter =
new EventWaitHandle(false, EventResetMode.AutoReset);
private Hardware hardware = new Hardware();
//what i'm doing now is holding a field for incoming event results
private string hardwareResult;
public MainViewModel()
{
hardware.OnHardwareMessage += hardware_OnHardwareMessage;
while (true)
{
hardware.Trigger();
if (hardwareWaiter.WaitOne(10000))
{
//is it possible to read the event argument here?
//do something with the event argument
someObservableCollection.Add(hardwareResult);
//clear the value
hardwareResult = string.Empty;
}
else
{
throw new Exception("Hardware did not respond on time");
}
}
}
//event listener for hardware events
private void hardware_OnHardwareMessage(string message)
{
hardwareResult = message;
hardwareWaiter.Set();
}
}
我所做的是触发设备并等待最多10秒钟的响应。我现在正在做的是持有一个类范围的字段,分配在事件监听器内接收的消息,在循环内部读取它并清除它。
我的问题是,是否有任何内置机制可以让我在EventWaitHandle
发出信号后直接读取事件参数(在事件监听器之外)。
答案 0 :(得分:0)
我不会以事件为基础。也许你的监听器应该公开一个代表传入消息流的BlockingCollection
。然后,读者可以在超时时从该集合中获取。如果超时命中没有消息被丢失(这是你现在的竞争条件)。
如果您坚持使用事件,请使用闭包来处理消息状态:
string hardwareResult = null;
Action handler = (...) =>
{
hardwareResult = message;
hardwareWaiter.Set();
};
hardware.OnHardwareMessage += handler;
这样你只需要一个比字段更紧密的局部变量。
通常的TCP错误也是如此:您假设读取整个消息。你可以一次读取一个字节。