Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:41:46+00:00 2026-06-05T12:41:46+00:00

I have a Sensor state machine for which I’ve written Cycle() methods: /// <summary>

  • 0

I have a Sensor state machine for which I’ve written Cycle() methods:

  /// <summary>
  /// Cycle sets the machine to follow a path from one it's current state to the next. The 
  /// behavior of the sensor is to revert to it's default state should an invalid state be
  /// encountered.
  /// </summary>
  /// <returns></returns>
  public IState Cycle() {
     if(_currentState.Next.IsNullOrEmpty()) {
        _currentState = DefaultState.Set();
     } else {
        _currentState = _currentState.Cycle();
     }
     return _currentState;
  }

  public IEnumerator<IState> Cycle(Func<bool> HasWork) {
     while(HasWork()) {
        yield return Cycle();
     }
  }

Implementation:

  [TestMethod]
  public void SensorExperiment_CycleWhileFunc() {
     float offset = .5f;
     IState previousState = State.Empty;

     IStimulus temp = new PassiveStimulus(68f) {
        Offset = offset
     };
     ISensor thermostat = new Sensor(65f, 75f, temp);

     int cycles = 0;
     // using this func to tell the machine when to turn off
     Func<bool> hasWork = () => {
        previousState = thermostat.CurrentState;
        // run 10 cycles6
        return cycles++ < 10;
     };

     var iterator = thermostat.Cycle(hasWork);

     while(iterator.MoveNext()) {
        Console.WriteLine("Previous State: {0}\tCurrent State: {1}",
           previousState.Name, iterator.Current.Name);
     }
  }

I have read Eric Lippert’s answer to an inquiry using the IEnumerator as a StateMachine. Is my implementation abusing or leveraging the use of an IEnumerator? I see my implementation as a way to provide automation of a sequence of states.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:41:48+00:00Added an answer on June 5, 2026 at 12:41 pm

    I don’t think using it like this is really an abuse, after all, you treat the result most as a collection.

    On the other hand, I don’t see any reason why you should use iterator here. If you rewrote your code to something like the following, it should work the same.

    while (hasWork())
        Console.WriteLine("Previous State: {0}\tCurrent State: {1}",
           previousState.Name, thermostat.Cycle().Name);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
I have a sensor which uses RS422 to spit out messages over serial. (I
I have a binary log file with streaming data from a sensor (Int16). Every
I have a temperature sensor, which is connected using an USB-I2C adapter ( http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
I have some tables in a MySQL database to represent records from a sensor.
I have NSMutable array that holds data captured from external sensor through iphone. I
I have a GUI app which connects to a sensor, gathers data and processes
I have a captured a data from a displacement sensor, the delta values for
I have a (soft) realtime system which queries some sensor data, does some processing
I have sensor data that I've calculated from an Android phone. I take and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.