如何用c#编写状态机?

发布于 2024-08-06 04:05:54 字数 128 浏览 4 评论 0原文

我需要用 C# 编写快速运行的状态机。 我喜欢 Windows Workflow Foundation 库,但它太慢并且功能过于拥挤(即繁重)。我需要更快的东西,最好是使用图形实用程序来设计图表,然后吐出 C# 代码。 有什么建议吗? 谢谢!

I need to write state machines that run fast in c#.
I like the Windows Workflow Foundation library, but it's too slow and over crowded with features (i.e. heavy). I need something faster, ideally with a graphical utility to design the diagrams, and then spit out c# code.
Any suggestions?
Thanks!

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

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

发布评论

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

评论(2

蓝眼泪 2024-08-13 04:05:54

最终,您可能需要重新设计的 .NET 4.0 中的 WF 引擎,因为它速度更快,并提供流程图活动(不完全是状态机,但适用于大多数场景)和良好的设计器 UI 体验。但由于它尚未发布,目前这可能不是一个好的答案。

作为替代方案,您可以尝试 stateless,这是一个专门用于在 .NET 中创建状态机程序的库。它似乎没有提供用户界面,但看起来非常适合实现您的其他目标。

Ultimately, you probably want the newly redesigned WF engine in .NET 4.0, as it is much faster and provides a flowchart activity (not quite a state machine, but works for most scenarios) and a nice designer UI experience. But since it's not yet released, that is probably not a good answer for now.

As an alternative, you could try stateless, a library specifically for creating state machine programs in .NET. It doesn't appear to provide a UI, but looks well-suited to fulfill your other goals.

锦上情书 2024-08-13 04:05:54

是的,微软在状态机 WF 方面可能走在了时代的前面。顺序工作流程的接受度要好得多。

当我们决定使用状态机时,我们推出了自己的状态机。因为我们找不到带有 UI 的可接受的框架。这是我们的步骤。希望他们能帮助你。

  1. 创建您的状态接口:

    公共接口IApplicationState
    {
        无效 ClickOnAddFindings();        
        无效 ClickOnViewReport();
        //依此类推
    }
    
  2. 创建状态并让它们实现接口:

    公共类 AddFindingsState : IApplicationState
    {
        frmMain _mForm;
    
        公共 AddFindingsState(frmMain mForm)
        {
            this._mForm = mForm;
        }
    
        公共无效ClickOnAddFindings()
        {            
        }
    
        公共无效ClickOnViewReport()
        {
            // 设置状态
            _mForm.SetState(_mForm.GetViewTheReportState());
        }
    }
    
  3. 在 main 中实例化状态
    类。

    IApplicationState _addFindingsState;
    IApplicationState _viewTheReportState;
    _addFindingsState = new AddFindingsState(this);
    _viewTheReportState = new ViewTheReportState(this);
    
  4. 当用户执行需要更改状态的操作时,调用方法来设置状态:

    _state.ClickOnAFinding();
    

当然,操作将存在于 IApplicationState 的特定实例中。

Yeah, Microsoft may have been ahead of their time with State Machine WF. Sequential Workflows are being received much better.

When we decided on using a state machine, we rolled our own. because we couldn't find an acceptable framework with a UI. Here are our steps. Hope they'll help you.

  1. Create your state interface:

    public interface IApplicationState
    {
        void ClickOnAddFindings();        
        void ClickOnViewReport();
        //And so forth
    }
    
  2. Create the states and have them implement the interface:

    public class AddFindingsState : IApplicationState
    {
        frmMain _mForm;
    
        public AddFindingsState(frmMain mForm)
        {
            this._mForm = mForm;
        }
    
        public void ClickOnAddFindings()
        {            
        }
    
        public void ClickOnViewReport()
        {
            // Set the State
            _mForm.SetState(_mForm.GetViewTheReportState());
        }
    }
    
  3. Instantiate the states in your main
    class.

    IApplicationState _addFindingsState;
    IApplicationState _viewTheReportState;
    _addFindingsState = new AddFindingsState(this);
    _viewTheReportState = new ViewTheReportState(this);
    
  4. When the user does something requiring a change of state, call the methods to set the state:

    _state.ClickOnAFinding();
    

Of course, the actions will live in the particular instance of the IApplicationState.

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