-1

I have a stateMachine where I need to download a huge amount of data in the background at State.LoadingOrderInfos. While my application is downloading the stuff (in the background), the operator should work on and go through the next states. At State.InsertPcbs I need the downloaded data.

In short words. I want to call LoadECMDataAsync when entering State.LoadingOrderInfos and it should not disturb the normal workflow.

enter image description here

_machine.Configure(State.LoadingOrderInfos)
    .Ignore(Trigger.WtPresent)
    .Ignore(Trigger.WtNotPresent)
    .SubstateOf(State.CanLogOut)
    .Permit(Trigger.OrderLoadingFailed, State.OrderNotSelected)
    .Permit(Trigger.OrderLoadingComplete, State.OrderCheckSetup)
    .OnEntry(() =>
    {
        IsLoading = true;
        Wt = null;
    })
    // Is this the way how to do it?
    .OnEntry(() =>
    {
        LoadECMDataAsync();
    })
    .OnActivate(async () =>
    {
        if (await _LoadOrderInfos().ConfigureAwait(true))
        {
            _machine.Fire(Trigger.OrderLoadingComplete);
        }
        else
        {
            _machine.Fire(Trigger.OrderLoadingFailed);
        }
    })
    .OnExit(() => IsLoading = false);
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77

1 Answers1

0

I think I would create a service class that fetches the data you need, maybe you could use Task.Run int this class to start the process.

When it's done, it could set a flag that is checkable. Or you could make it so that it calls a callback.

Then use PermitIf to check if a flag has been set.

Your problem is ortogonal, so I think it makes sense to split up the responsibilities.

BTW, you are using OnActivate. This should only be used if you need to "Activate" the state machine, like if you reload it from storage. At the moment it works like OnEntry (so use that), but I'd like to change it...

HenningNT
  • 183
  • 8