我正在使用无状态框架来创建状态机。我的主应用程序是一个控制台应用程序,它调用DataManager
该类,该类调用GetData
启动 a 的函数,然后使用状态机Task
执行该函数。Receiver
我的问题是当 StateMachine 运行时我得到大约 50% 的 CPU 使用率!当我使用简单的 switch 语句和相同的 Task 实现 StateMachine 时,它以 0% 的 CPU 使用率运行!我有一些编码错误还是无状态框架的行为?
获取数据函数
public List<Byte[]> GetData()
{
List<Byte> rawData = new List<Byte[]>();
ReceiveTask = Task.Factory.StartNew<List<Byte[]>>(() => Receiver());
//Wait until the Task is complete
ReceiveTask.Wait();
//return temp;
rawData = ReceiveTask.Result;
return rawData;
}
数据管理器类
public partial class DataManager
{
int _expectedBlocks;
Byte[] _currentMessage = null;
Byte[] _firstMessage = null;
int _currentBlockNumber = 0;
enum State { Idle, Start, Next, Check, End }
enum Trigger { DataOK, DataRequest, ReceivingFirstBlock, ReceivingNextBlock, LastPacketReceived, WaitForNext }
Dictionary<int, Byte[]> _receivedData;
List<Byte[]> _outputList;
StateMachine<State, Trigger> _machine;
private List<Byte[]> Receiver()
{
_currentMode = Mode.Receive;
m_source.MessageReceivedEvent +=new EventHandler<WSANMessageResponseEventArgs>(m_source_MessageReceivedEvent);
m_source.StartConnection();
_machine = new StateMachine<State, Trigger>(State.Idle);
_receivedData = new Dictionary<int, byte[]>();
_outputList = new List<byte[]>();
//Config StateMachine
_machine.Configure(State.Idle)
.OnEntry(() => OnIdle())
.OnExit(() => ExitIdle())
.Permit(Trigger.ReceivingFirstBlock, State.Start);
_machine.Configure(State.Start)
.OnEntry(() => OnStart())
.Permit(Trigger.ReceivingNextBlock, State.Next);
_machine.Configure(State.Next)
.OnEntry(() => OnNext())
.Permit(Trigger.WaitForNext, State.Start)
.Permit(Trigger.LastPacketReceived, State.Check);
_machine.Configure(State.Check)
.OnEntry(() => OnCheck())
.Permit(Trigger.DataOK, State.End)
.Permit(Trigger.DataRequest, State.Idle);
_machine.Configure(State.End)
.OnEntry(() => OnEnd());
while (_machine.State != State.End)
{
}
_currentMode = Mode.Idle;
return _outputList;
}
问候迈克尔