State machine with different state and cancel option

Markus Freitag 3,791 Reputation points
2023-06-20T15:55:01.5333333+00:00

Hello,

enter image description here Optimization statemachine. I am looking for a sample solution.

If I enable Cancel, how can I run it again? With new var source = new CancellationTokenSource();

Does anyone have any good approaches? Thank you.

source.Cancel();

See my code in the attachment.

ProtoType_WinForms_cs.TXT

Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,086 Reputation points Volunteer Moderator
    2023-06-20T18:29:03.1266667+00:00

    your code is not clear. what triggers a cancel? also a state is usually:

    current state -> process input -> new state

    you are combining a state machine with background threads. what is the interaction? is the input process async? is the state machine a separate thread?

    you keep showing a transport belt with sensors. what do the sensor report? what is the actual use of each sensor. you show a product passing sensors, can a product be a sensor start and another at sensor end at the same time?


  2. Bruce (SqlWork.com) 78,086 Reputation points Volunteer Moderator
    2023-06-28T19:49:46.82+00:00

    you state machine makes no sense.

    you have 4 inputs per state:

    public class StateInputValues
    {
       int _InSensorOn;  
       int _StartSensorOn;
       int _OutSensorOn;
       int _CancelOn;
    
       public bool InSensorOn 
       {
          get {return _InSensorOn == 1;}
          set 
          { 
             if (value) Interlocked.Or(ref _InSensorOn,1);  
             else Interlocked.And(ref _InSensorOn,0);
          }
       }
       ....
    
    }
    

    each state should map the 4 inputs to the next state. each state can have optional processing.

    other than a background process thread to free the ui you haven't explained the need for threads.

    I would assume threads are used to get the status of the sensors, but this is outside the state loop. you just need to use locks or the InterLock class to update the sensor values. but your ui is performing this in you prototype.

    the actual send data to printer is async with a timeout, but this could be exposed as sync. it probably add a new state input variable for communication error (maybe for all sensors).


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.