So, I am making a program designed to complete some automated process. I have a form that allows the user to build custom automation processes. The structure of this automation process is as follows:
public class AutomationScript
{
public string FilePath;
public List<AutomationEvent> Events;
public AutomationScript()
{
Events = new List<AutomationEvent>();
}
}
public class AutomationCommand
{
public AutomationCommandType Type;
public int Parameter;
public AutomationCommand(AutomationCommandType _type, int _parameter)
{
Type = _type;
Parameter = _parameter;
}
}
public struct AutomationEvent
{
public string Name;
public bool LoopEnabled;
public List<AutomationCommand> Commands;
}
public enum AutomationCommandType
{
WaitSecs,
CollectSample,
CollectBackGround,
StopCollection,
LoopStart,
LoopEnd,
}
Now, when the user initiates the process to run a thread is created and it iterates through the commands. The basic setup is as follows:
private void runAutomationScript()
{
while (automationON)
{
foreach (AutomationEvent aEvent in fullAutomationScript.Events)
{
int loopStartIndex = 0;
int loopEndIndex = 0;
foreach (AutomationCommand command in aEvent.Commands)
{
while (!CommandCompleted) { } //Will stay in loop until the hardware module is finished receiving spectra
switch (command.Type)
{
case AutomationCommandType.CollectBackGround:
RequestedBackgroundScans = command.Parameter;
spectralCollection.getBackGroundSpectra(RequestedBackgroundScans);
CommandCompleted = false;
break;
case AutomationCommandType.CollectSample:
RequestedSampleScans = command.Parameter;
spectralCollection.getSampleSpectra(RequestedSampleScans);
CommandCompleted = false;
break;
case AutomationCommandType.WaitSecs:
Thread.Sleep(command.Parameter * 1000);
break;
case AutomationCommandType.StopCollection:
automationON = false;
break;
case AutomationCommandType.LoopStart:
loopStartIndex = aEvent.Commands.IndexOf(command);
break;
case AutomationCommandType.LoopEnd:
loopEndIndex = aEvent.Commands.IndexOf(command);
//Wehn the loop end is reached it will get the index of the start and end and grab a sublist and iterate over that continuously.
int firstCommandIndex = loopStartIndex + 1;
List<AutomationCommand> loopedCommands = aEvent.Commands.GetRange(firstCommandIndex, loopEndIndex - firstCommandIndex );
break;
}
}
}
}
OnAutomationFinished(EventArgs.Empty);
}
Now, at this point it works fine. I am trying to allow the user to define a loop point for the process to loop through. As you can see there is an AutomationCommand called LoopStart and LoopEnd. I am trying to figure out how to loop through this second sub loop without copying code into another function. I am toying with the idea of making some recursive definition of this command loop. Any ideas will help.
CommandCompletedis because the spectral collection is done by events. So, when the commands to get spectral data are initiated the loop must wait until these events are done firing.