Task Control Restriction Example Program

This AeroScript program shows you how to use TaskControlRestrict().

// Task Control Restriction Example (TaskControlRestriction.ascript):
// This example shows how to restrict access to tasks to prevent interference with critical tasks.
// Call the TaskControlRestrict() function to prevent Automation1 Studio, other AeroScript tasks,
// or the Automation1 APIs from doing specific operations. The operations you can restrict include:
// pause or stop execution, MFO/MSO immediate commands, feedhold, and debugging features
// such as breakpoints. This example restricts the task on which the program is running before it
// enters a critical section of code.

program

	// Set ErrorHandler to run if an error occurs.
	onerror(ErrorHandler())

	// Restrict program operations that an operator can do on a task.
	TaskControlRestrict(TaskGetIndex())

	// Configure the task to not cause an error on an axis fault.
	ParameterSetTaskValue(TaskGetIndex(), TaskParameter.TaskTerminationAxes, 0x0)

	// This background task executes the code inside the loop one time each millisecond.
	CriticalSectionStart()
	while (true)

		// The main background task code goes here.
		DoSomethingImportant()

		// Dwell for 1 millisecond to give time to other task programs
		// because this is a critical section.
		Dwell(0.001)
	end
	CriticalSectionEnd()

	// NOTE: TaskControlAllow() continues the task operations that you prevented with TaskControlRestrict(),
	// which includes stopping the task.

end

// This function shows an important task that should not be interrupted.
function DoSomethingImportant()
	$iglobal[0] = $iglobal[0] + 1
end

// This function is registered with onerror() and will run if an error occurs.
function ErrorHandler()
	// Clear the task errors.
	TaskClearError(TaskGetIndex())

	// Restart the program after you clear the errors.
	Dwell(0.001)
	ProgramRestart()
end