Application: MultithreadedCalculation
Revit Platform: All
Revit Version: 2012.0
First Released For: 2012.0
Programming Language: C#
Skill Level: Advanced
Category: Geometry
Type: ExternalCommand

Subject: Analysis Visualization Framework	
Summary: A combination of Analysis Visualization Framework, multi-threading, Revit’s Idling event, and Dynamic Model Update to simulate how analysis results can be periodically updated during computation and re-started when the Revit model changes.

Classes: 
Autodesk.Revit.DB.Analysis.FieldDomainPointsByUV
Autodesk.Revit.DB.Analysis.FieldValues
Autodesk.Revit.DB.Analysis.SpatialFieldManager
Autodesk.Revit.DB.UpdaterRegistry
System.Threading.Thread

Project Files:
MultithreadedCalculation.cs

Description: 
1.	Prompt the user to select an element with Selection.PickObject
2.	Find the largest face of the element
3.	Get the active view’s SpatialFieldManager, or create a SpatialFieldManager if the active view does not have one.
4.	Add the reference of the element’s largest face to the SpatialFieldManager.
5.	Find the minimum and maximum UV of the face’s bounding box
The myUV class is used because it can be safely passed to the external thread. myUV contains two double values. Native Revit classes (such as Autodesk.Revit.DB.UV) cannot be given to the thread.
6.	Create and register an IUpdater (SpatialFieldUpdater) so that the command will be able to respond when the element selected in step 1 is changed or deleted.
7.	Register an Idling event. Revit will use this event to alert the command that Revit is ready to receive and display additional results data.
8.	Start a new thread that will be used to compute and display the results
a.	Populate an IList of myUV elements describing the locations on the surface where results will be calculated
b.	Compute results
i.	Results are stored by adding instances of the resultData class to the “results” IList. Each resultData instance contains a myUV and double value.
ii.	The results list is locked when it is being modified, to prevent it from being modified by multiple threads at the same time.
iii.	For the sake of simplicity in this example, the current second from the DateTime.Now property is used.
iv.	Thread.Sleep is used to create a ½ second pause, simulating the time that might be needed to perform a complex analysis, and to make the various features of this example more apparent.

Instructions: 
1.	Open MultithreadedCalculation.rvt
2.	Run the external command
3.	Select the mass, floor, or wall in the sample file
4.	Revit will be adding results data to the largest face of the selected element
5.	At any time (while the calculation is running or after it is complete), modify the geometry of the element. The previous result data will be removed and new results data will be calculated.