NET4: Script Caching
NET4: Script Caching
- Manage script lifetimes
- BasicIdeCtl synchronized with a BasicNoUIObj in the same thread
- Application evaluates function defined by script
- DesignMode controls script editing/evaluation for the application
- NET 4 Sample Implementation
NET4: Script Caching
For an explanation of how this sample works please refer to the
Script Caching solution.
Sample source: ScriptCaching-Net4.zip
Load a script:
public ScriptNode LoadScript(string script_name)
{
if (!script_name.StartsWith(@"\"))
{
// relative script path
if (active_script_node_ != null)
script_name = Combine(active_script_node_.ScriptName, script_name);
else
script_name = @"\" + script_name;
}
// is the script already loaded
ScriptNode script_node = loaded_script_nodes_.Find(script_nodex => script_nodex.ScriptName == script_name);
if (script_node == null)
// not loaded
script_node = new ScriptNode(script_name);
// evaluate a script node
if (!script_node.IsLoaded)
{
// load the script, a syntax error will throw an exception
script_node.Load(basicNoUIObj_, Fast);
// add to the front of the loaded list
loaded_script_nodes_.Insert(0, script_node);
// update tree view with new status
ScriptLoadedStatusChanged?.Invoke(script_name, true);
while (loaded_script_nodes_.Count > max_loaded_scripts)
{
// remove the last (oldest loaded script node)
var oldest_script_node = loaded_script_nodes_[loaded_script_nodes_.Count-1];
if (oldest_script_node.IsActive)
break;
loaded_script_nodes_.RemoveAt(loaded_script_nodes_.Count - 1);
oldest_script_node.Unload();
// update tree view with new status
ScriptLoadedStatusChanged?.Invoke(oldest_script_node.ScriptName, false);
}
}
else if (script_node != loaded_script_nodes_[0])
{
// move to the front of the loaded list
loaded_script_nodes_.Remove(script_node);
loaded_script_nodes_.Insert(0, script_node);
}
return script_node;
}
Evaluate a script:
public object EvaluateScript(ScriptNode script_node, object default_value = null)
{
// script is active
ScriptNode last_active_script_node = active_script_node_;
active_script_node_ = script_node;
try
{
// a script run-time error will throw an exception
return active_script_node_.Evaluate(default_value);
}
finally
{
// script is no longer active
active_script_node_ = last_active_script_node;
}
}
Copyright Polar Engineering, Inc.