验证活动[英] Validate a raising of event

本文是小编为大家收集整理的关于验证活动的处理方法,想解了验证活动的问题怎么解决?验证活动问题的解决办法?验证活动问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我试图验证事件的提升:

我有一个设备,它在发送命令后将其返回给我一些消息.

我有一个事件,当我在串行端口上收到新消息时正在加剧.

我恢复了此事件,我想在将另一个命令发送到我的设备之前对此升级进行验证.

Public Constructor()
{
 ModuleProtocole.MessageReceive += SendValidate;
}

SendValidate()
{
//maybe something to write here?
}

SendNewMessage()
{
 ModuleProtocole.SendMessage ("Version");
 // if SendValidate() is call because of event go to next line
 ModuleProtocole.SendMessage ("Value1");
}

推荐答案

如果我理解这个问题并给出最简单的答案.

private bool? _hasEventBeenRaised;

private void SendValidate()
{
   _hasEventBeenRaised = true;
}

private void SendNewMessage()
{
   if(_hasEventBeenRaised == false)
   {
      Console.WriteLine("No event received");
      return;
   }
   
   _hasEventBeenRaised = false;

   // send message
    
}

注释:此外,如果在另一个线程上提出事件,您将需要使用lock来确保没有线程问题.

过于谨慎的锁定方法

private bool? _hasEventBeenRaised;
private object _sync = new object();

private void SendValidate()
{
   lock(_sync)
   {
      _hasEventBeenRaised = true;
   }
}

private void SendNewMessage()
{
    lock(_sync)
    {
       if(_hasEventBeenRaised == false)
       {
           Console.WriteLine("No event received");
           return;
       }
   
       _hasEventBeenRaised = false;

       // send message
    }
    
}

其他推荐答案

为什么不使用sendValidate函数(设备响应第一消息时调用)来执行第二条消息的发送?

,但理想情况下,我会使用一些信号进行操作,所以基本上我会创建一条发送消息,该消息会阻止 - 等待信号(最初应该是免费的),然后锁定信号(信号量),然后发送消息.然后,消息回调事件应释放信号.

https .microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim?view = netcore-3.1

var semaphore = new SemaphoreSlim(1, 1); // 1 available call, max. 1 concurrent calls

SendMessage()
{
  this.semaphore.WaitOne();
  // NOTE: add send message here
}

MessageReceived()
{
  this.semaphore.Release(1);
}

本文地址:https://www.itbaoku.cn/post/2352222.html

问题描述

I try to validate a raising of event:

I have a Device which return me some message after I send it a command.

I have an event which is raise when I got new message on my serial port.

I recover this event and I want to do a validation of this raising before sending an other command to my Device.

Public Constructor()
{
 ModuleProtocole.MessageReceive += SendValidate;
}

SendValidate()
{
//maybe something to write here?
}

SendNewMessage()
{
 ModuleProtocole.SendMessage ("Version");
 // if SendValidate() is call because of event go to next line
 ModuleProtocole.SendMessage ("Value1");
}

推荐答案

If I understand the question, and to give the simplest possible answer.

private bool? _hasEventBeenRaised;

private void SendValidate()
{
   _hasEventBeenRaised = true;
}

private void SendNewMessage()
{
   if(_hasEventBeenRaised == false)
   {
      Console.WriteLine("No event received");
      return;
   }
   
   _hasEventBeenRaised = false;

   // send message
    
}

Note : This disregaurds any other issue, additionally if the event is raised on another thread, you will want to use lock to ensure there is no threading issues.

An overly cautious lock approach

private bool? _hasEventBeenRaised;
private object _sync = new object();

private void SendValidate()
{
   lock(_sync)
   {
      _hasEventBeenRaised = true;
   }
}

private void SendNewMessage()
{
    lock(_sync)
    {
       if(_hasEventBeenRaised == false)
       {
           Console.WriteLine("No event received");
           return;
       }
   
       _hasEventBeenRaised = false;

       // send message
    }
    
}

其他推荐答案

Why not just use the SendValidate function (which is a called when the device responds to the 1st message) to do the sending of the second message?

But ideally i'd do it with some signals, so basically i'd create a send message, which blocks - waits for a signal (initially should be free), then locks the signal (semaphore) and then sends the message. The message callback event then should free the signal.

https://docs.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim?view=netcore-3.1

var semaphore = new SemaphoreSlim(1, 1); // 1 available call, max. 1 concurrent calls

SendMessage()
{
  this.semaphore.WaitOne();
  // NOTE: add send message here
}

MessageReceived()
{
  this.semaphore.Release(1);
}
查看更多