状态机问题

发布于 2024-09-28 14:04:18 字数 732 浏览 10 评论 0原文

在同一函数中有一个具有两个转换的状态机。

static readonly object _object = new object();

lock (_object)
{
    // I want Host received the event of SMTrans01 first .
    Obj.StateMachine.Send((int)MyStateMachine.EventType.SMTrans01, new object[2] { Obj, MyStateMachine.EventType.SMTrans01 });
}

lock (_object)
{
    // And then I want Host received the event of SMTrans02.
    Obj.StateMachine.Send((int)MyStateMachine.EventType.SMTrans02, new object[2] { Obj, MyStateMachine.EventType.SMTrans02 });
}

我按照上面的方式实现了我的状态机代码。我不确定我是否正确理解 Lock 语句?

我需要事件遵循正确的顺序(主机首先收到 SMTrans01,然后主机收到 SMTrans02 事件)。

经过测试,我发现有时主机会先收到 SMTrans02 事件。看起来 Lock 语句不起作用。我不知道为什么。

有什么好的方法来应对吗?

There is a State Machine with two Transitions in the same function.

static readonly object _object = new object();

lock (_object)
{
    // I want Host received the event of SMTrans01 first .
    Obj.StateMachine.Send((int)MyStateMachine.EventType.SMTrans01, new object[2] { Obj, MyStateMachine.EventType.SMTrans01 });
}

lock (_object)
{
    // And then I want Host received the event of SMTrans02.
    Obj.StateMachine.Send((int)MyStateMachine.EventType.SMTrans02, new object[2] { Obj, MyStateMachine.EventType.SMTrans02 });
}

I implemented my state machine code as above. I am not sure I understand Lock statement correctly or not?

I need the events followed the the right order (Host received SMTrans01 first, and then host received SMTrans02 event).

After testing I found sometime host will receive the SMTrans02 event first. Looks like Lock statement not work. I don't know why.

Are there any good methods to approach it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北斗星光 2024-10-05 14:04:18

看来您的问题与线程和锁定无关。

我怀疑您的 Send 方法是异步的。解决方案是改用同步方法。在收到第一个事件已处理的确认之前,不要发送第二个事件。

或者,重写您的接收代码,以便它可以处理不按顺序出现的事件。

如果您提供 Send 的代码并详细描述如何调用您的方法,这将有助于调试问题。

It looks like your problem has nothing to do with threads and locking.

I suspect that your Send method is asynchronous. The solution is to use a synchronous approach instead. Do not send the second event until you get an acknowledgement that the first event has been handled.

Alternatively, rewrite your receiving code so that it can handle the events coming out of order.

If you provide the code for Send and describe more how your method is being called that would help debugging the problem.

橙味迷妹 2024-10-05 14:04:18

如果顺序

EventType _state = EventType.SMTrans02;
if(_state == EventType.SMTrans02 )
{
 _state =EventType.SMTrans01;
  Obj.StateMachine.Send((int)_state, new object[2] { Obj, _state });

}
else
{
_state = EventType.SMTrans02;
Obj.StateMachine.Send((int)_state, new object[2] { Obj, _state });
}

对更复杂的情况很重要,您可以使用 switch 块,甚至使用

您需要 Lock 的 状态模式仅同步可能调用这些事件的线程。

if the order matters

EventType _state = EventType.SMTrans02;
if(_state == EventType.SMTrans02 )
{
 _state =EventType.SMTrans01;
  Obj.StateMachine.Send((int)_state, new object[2] { Obj, _state });

}
else
{
_state = EventType.SMTrans02;
Obj.StateMachine.Send((int)_state, new object[2] { Obj, _state });
}

for more complex situation you may use switch block or even use State Pattern

you need Lock only to sync the threads which may call those events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文