This document describes the PX1008 diagnostic.
Code | Short Description | Type | Code Fix |
---|---|---|---|
PX1008 | The reference to the screen graph or an action adapter in the long-running operation and processing delegates causes synchronous execution of the delegate. | Error | Unavailable |
PXLongOperation
delegates should not contain references to the screen graph or an action adapter of the PXAdapter
type because such references cause synchronous execution of the delegate.
The diagnostic uses data flow analysis that considers the passed data. This allows the diagnostic to check the parameters passed to other graph and helper methods. For example, if a developer passes a screen graph or an action adapter as an argument to a method and invokes in this method a long-running operation that uses the passed arguments, this diagnostic captures such code. Corner cases not captured by the data flow analysis exist because live diagnostics have to limit resource usage and some things cannot be checked at compile time. But in our practice, these cases are rarely encountered in the production code.
The following section describes the principles of the data flow analysis used by the main walker of the PX1008 diagnostic. The walker maintains a stack of non-capturable elements. Before the walker visits a method from a method call recursively, the walker calculates what can capture a reference to a graph or an adapter at the call site. The diagnostic supports the following options:
- When the walker is at the top of the call stack and inside an action handler. In this case, the adapter parameter can be captured.
- When the walker is inside a graph or a graph extension and
this
is passed among the arguments. - When the walker is inside a recursive call and already has some non-capturable parameters that are used in call arguments.
- When the walker is inside a local method and parameters from containing methods have some non-capturable elements.
Additional walkers perform the following checks:
- Checks for parameters and
this
captured in arguments expression - Checks for reassignments of parameters before the call site
To fix the issue, you rewrite the delegate so that it does not contain references to the screen graph or the action adapter.
The diagnostic also validates delegates passed to the PXLongOperation.StartOperation()
method. Thus, you can find captured references to the graph instance and action adapters in the delegate closures.
public class SomeGraph : PXGraph<SomeGraph>
{
public PXFilteredProcessing<SomeDAC> Processing;
public SomeGraph()
{
Processing.SetProcessDelegate(item => this.Process(item)); // The PX1008 error is displayed for this line.
}
private Process(SomeDAC item) { }
}
Suppose that a helper class contains the following methods:
CaptureInLongRun
which initiates a long-running operation with a graph instance in the delegateCaptureInLongRunArray
which initiates a long-running operation that captures the array of objects passed to it in the delegate
The diagnostic will show a warning, as shown in the following code.
public IEnumerable someAction(PXAdapter adapter)
{
var helper = new NonGraph();
helper.CaptureInLongRun(this); // The PX1008 error is displayed for this line.
helper.CaptureInLongRunArray(new[] { adapter }); // The PX1008 error is displayed for this line.
return adapter.Get();
}