How to optimize method's that track metrics in 3rd party application?
Posted
by
WulfgarPro
on Stack Overflow
See other posts from Stack Overflow
or by WulfgarPro
Published on 2011-01-10T00:52:36Z
Indexed on
2011/01/10
0:53 UTC
Read the original article
Hit count: 120
Hi,
I have two listboxes that keep updated lists of various objects roaming in a 3rd party application.
When a user selects an object from a listbox, an event handler is fired, calling a method that gathers various metrics belonging to that object from the 3rd party application for displaying in a set of textboxes. This is slow! I am not sure how to optimize this functionality to facilitate greater speeds..
private void lsbUavs_SelectedIndexChanged(object sender, EventArgs e)
{
if (_ourSelectedUavFromListBox != null)
{
UtilStkScenario.ChangeUavColourOnScenario(_ourSelectedUavFromListBox.UavName, false);
}
if (lsbUavs.SelectedItem != null)
{
Uav ourUav = UtilStkScenario.FindUavFromScenarioBasedOnName(lsbUavs.SelectedItem.ToString());
hsbThrottle.Value = (int)ourUav.ThrottleValue;
UtilStkScenario.ChangeUavColourOnScenario(ourUav.UavName, true);
_ourSelectedUavFromListBox = ourUav;
// we don't want this thread spawning many times
if (tUpdateMetricInformationInTabControl != null)
{
if (tUpdateMetricInformationInTabControl.IsAlive)
{
tUpdateMetricInformationInTabControl.Abort();
}
}
tUpdateMetricInformationInTabControl = new Thread(UpdateMetricInformationInTabControl);
tUpdateMetricInformationInTabControl.Name = "UpdateMetricInformationInTabControlUavs";
tUpdateMetricInformationInTabControl.IsBackground = true;
tUpdateMetricInformationInTabControl.Start(lsbUavs);
}
}
delegate string GetNameOfListItem(ListBox listboxId);
delegate void SetTextBoxValue(TextBox textBoxId, string valueToSet);
private void UpdateMetricInformationInTabControl(object listBoxToUpdate)
{
ListBox theListBoxToUpdate = (ListBox)listBoxToUpdate;
GetNameOfListItem dGetNameOfListItem = new GetNameOfListItem(GetNameOfSelectedListItem);
SetTextBoxValue dSetTextBoxValue = new SetTextBoxValue(SetNamedTextBoxValue);
try
{
foreach (KeyValuePair<string, IAgStkObject> entity in UtilStkScenario._totalListOfAllStkObjects)
{
if (entity.Key.ToString() == (string)theListBoxToUpdate.Invoke(dGetNameOfListItem, theListBoxToUpdate))
{
while ((string)theListBoxToUpdate.Invoke(dGetNameOfListItem, theListBoxToUpdate) == entity.Key.ToString())
{
if (theListBoxToUpdate.Name == "lsbEntities")
{
double[] latLonAndAltOfEntity = UtilStkScenario.FindMetricsOfStkObjectOnScenario(UtilStkScenario._stkObjectRoot.CurrentTime, entity.Value);
SetEntityOrUavMetricValuesInTextBoxes(dSetTextBoxValue, "Entity", entity.Key, "",
"", "", "", latLonAndAltOfEntity[4].ToString(), latLonAndAltOfEntity[3].ToString());
}
else if (theListBoxToUpdate.Name == "lsbUavs")
{
double[] latLonAndAltOfEntity = UtilStkScenario.FindMetricsOfStkObjectOnScenario(UtilStkScenario._stkObjectRoot.CurrentTime, entity.Value);
SetEntityOrUavMetricValuesInTextBoxes(dSetTextBoxValue, "UAV", entity.Key, entity.Value.ClassName.ToString(),
latLonAndAltOfEntity[0].ToString(), latLonAndAltOfEntity[1].ToString(), latLonAndAltOfEntity[2].ToString(),
latLonAndAltOfEntity[4].ToString(), latLonAndAltOfEntity[3].ToString());
}
}
}
}
}
catch (Exception e)
{
// selected entity was deleted(end-of-life) in STK - remove LLA information from GUI
if (theListBoxToUpdate.Name == "lsbEntities")
{
SetEntityOrUavMetricValuesInTextBoxes(dSetTextBoxValue, "Entity", "", "", "", "", "", "", "");
UtilLog.Log(e.Message.ToString(), e.GetType().ToString(), "UpdateMetricInformationInTabControl",
UtilLog.logWriter);
}
else if (theListBoxToUpdate.Name == "lsbUavs")
{
SetEntityOrUavMetricValuesInTextBoxes(dSetTextBoxValue, "UAV", "", "", "", "", "", "", "");
UtilLog.Log(e.Message.ToString(), e.GetType().ToString(), "UpdateMetricInformationInTabControl",
UtilLog.logWriter);
}
}
}
internal static double[] FindMetricsOfStkObjectOnScenario(object timeToFindMetricState, IAgStkObject stkObject)
{
double[] stkObjectMetrics = null;
try
{
stkObjectMetrics = new double[5];
object latOfStkObject, lonOfStkObject;
double altOfStkObject, headingOfStkObject, velocityOfStkObject;
IAgProvideSpatialInfo spatial = stkObject as IAgProvideSpatialInfo;
IAgVeSpatialInfo spatialInfo = spatial.GetSpatialInfo(false);
IAgSpatialState spatialState = spatialInfo.GetState(timeToFindMetricState);
spatialState.FixedPosition.QueryPlanetodetic(out latOfStkObject, out lonOfStkObject, out altOfStkObject);
double[] stkObjectheadingAndVelocity = FindHeadingAndVelocityOfStkObjectFromScenario(stkObject.InstanceName);
headingOfStkObject = stkObjectheadingAndVelocity[0];
velocityOfStkObject = stkObjectheadingAndVelocity[1];
stkObjectMetrics[0] = (double)latOfStkObject;
stkObjectMetrics[1] = (double)lonOfStkObject;
stkObjectMetrics[2] = altOfStkObject;
stkObjectMetrics[3] = headingOfStkObject;
stkObjectMetrics[4] = velocityOfStkObject;
}
catch (Exception e)
{
UtilLog.Log(e.Message.ToString(), e.GetType().ToString(), "FindMetricsOfStkObjectOnScenario",
UtilLog.logWriter);
}
return stkObjectMetrics;
}
private static double[] FindHeadingAndVelocityOfStkObjectFromScenario(string stkObjectName)
{
double[] stkObjectHeadingAndVelocity = new double[2];
IAgStkObject stkUavObject = null;
try
{
string typeOfObject = CheckIfStkObjectIsEntityOrUav(stkObjectName);
if (typeOfObject == "UAV")
{
stkUavObject = _stkObjectRootToIsolateForUavs.CurrentScenario.Children[stkObjectName];
IAgDataProviderGroup group = (IAgDataProviderGroup)stkUavObject.DataProviders["Heading"];
IAgDataProvider provider = (IAgDataProvider)group.Group["Fixed"];
IAgDrResult result = ((IAgDataPrvTimeVar)provider).ExecSingle(_stkObjectRootToIsolateForUavs.CurrentTime);
stkObjectHeadingAndVelocity[0] = (double)result.DataSets[1].GetValues().GetValue(0);
stkObjectHeadingAndVelocity[1] = (double)result.DataSets[4].GetValues().GetValue(0);
}
else if (typeOfObject == "Entity")
{
IAgStkObject stkEntityObject = _stkObjectRootToIsolateForEntities.CurrentScenario.Children[stkObjectName];
IAgDataProviderGroup group = (IAgDataProviderGroup)stkEntityObject.DataProviders["Heading"];
IAgDataProvider provider = (IAgDataProvider)group.Group["Fixed"];
IAgDrResult result = ((IAgDataPrvTimeVar)provider).ExecSingle(_stkObjectRootToIsolateForEntities.CurrentTime);
stkObjectHeadingAndVelocity[0] = (double)result.DataSets[1].GetValues().GetValue(0);
stkObjectHeadingAndVelocity[1] = (double)result.DataSets[4].GetValues().GetValue(0);
}
}
catch (Exception e)
{
UtilLog.Log(e.Message.ToString(), e.GetType().ToString(), "FindHeadingAndVelocityOfStkObjectFromScenario",
UtilLog.logWriter);
}
return stkObjectHeadingAndVelocity;
}
Any help would be really appreciated. From my knowledge, I cant really see any issues with the C#. Maybe it has to do with the methodology I'm using.. maybe some kind of caching mechanism is required - this is not natively available.
WulfgarPro
© Stack Overflow or respective owner