DynamoDS/Dynamo — pull request #2624 by Benglin (2014-10-06 15:33:50+00:00)
Title: Implemented two new async task classes

Background
-----
This pull request isolates the two new ```AsyncTask``` derived classes for earlier review (overall code that consumes these new tasks can be found on [a separate branch](https://github.com/Benglin/Dynamo/compare/Vizcheduler)):

- ```UpdateRenderPackageAsyncTask```
- ```AggregateRenderPackageAsyncTask```

In the existing visualization process, there are two phases:

1. Render package creation phase
2. Visual update phase

The first of these two phases is done by ```RenderManager.RenderExec``` with support from ```NodeModel``` (i.e. ```UpdateRenderPackage``` method). The second phase was mostly done in ```AggregateUpstreamRenderPackages``` method or ```VisualizationManager```.

While these worked for most of the time, they had several issues:

1. ```RenderManager.RenderExec``` runs on a background thread that is not synchronized with the thread that interacts with the virtual machine. The thread safety is not guaranteed between these threads, especially when it comes to ```MirrorData``` and ```IGraphicItem``` access.

2. ```RenderManager.RenderExec``` method accesses ```WorkspaceModel.Nodes``` collection directly from the background thread, causing rare but possible sharing violation when the ```Nodes``` collection can be freely updated by the UI thread.

3. ```AggregateUpstreamRenderPackages``` method (or any method that deals with gathering ```IRenderPackage``` objects from ```NodeModel``` objects) always run on the UI thread, unnecessarily blocking the UI for the duration of gathering the ```IRenderPackage``` objects.

Solution
-----
The two phases of visualization process are now performed in the following ```AsyncTask``` derived classes:

1. ```UpdateRenderPackageAsyncTask``` which generates ```IRenderPackage``` objects for a given ```NodeModel``` in the context of ```ISchedulerThread```. The scheduler ensures this task is executed while the virtual machine is not accessing the same underlying data.

2. ```AggregateRenderPackageAsyncTask``` which gathers ```IRenderPackage``` objects in the context of ```ISchedulerThread```, which is independent of UI thread.

Hi @ikeough, you are most familiar with the original visualization code, please have a look. Thanks!