/// <summary> /// Initializes Sherlock and then loads the supplied investigation. /// </summary> /// <param name="investigationPath">Path and name of Sherlock investigation to be loaded.</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="DirectoryNotFoundException"></exception> /// <exception cref="ExternalException"></exception> public void InitializeAndLoadInvestigation(string investigationPath) { if (investigationPath == null) { throw new ArgumentNullException( "investigationPath", "The value of \"string investigation\" passed to the \"LoadInvestigation( string investigation )\" method was null."); } if (!File.Exists(investigationPath)) { throw new DirectoryNotFoundException( string.Format("The supplied Sherlock path {0} could not be found", investigationPath)); } // Get UI context to marshal messages from // a worker thread back to the main UI thread. _synchronizationContext = SynchronizationContext.Current; // Because loading the Sherlock investigation takes so long // the UI would deadlock during the loading process. I now // instantiate the sherlock engine and load the investigation // in another thread to prevent the UI from deadlocking. Task.Factory.StartNew(() => { InitializeEngine( ); _iReturn = _sherlock.InvLoad(investigationPath); }).ContinueWith(task => { // Execute code in the main UI context. _synchronizationContext.Post(callback => AfterInitializationAndInvestigationLoad( ), null); }); }
/// <summary> /// Saves the Sherlock investigation. /// </summary> /// <param name="investigationPath">The full path and name of the investigation including the .ivs file extention.</param> /// <exception cref="ArgumentException"></exception> public void SaveInvestigationChanges(string investigationPath) { _iReturn = _sherlock.InvSave(investigationPath); if (_iReturn != I_ENG_ERROR.I_OK) { throw new ArgumentException( string.Format("An error was encountered while save the investigation. Sherlock return: {0}", _iReturn)); } }
// Halt Sherlocks mode of operation. In order to avoid // using Dalsa's method of halting the investigation // which involed using Application.DoEvents() I set and // wait for Sherlock to halt in another task. This prevents // the main UI thread from deadlocking and not updating _iCurrMode. /// <summary> /// Set Sherlocks mode to halt. /// </summary> public void Halt( ) { // Halt Sherlock and wait for halted state in new task. Task.Factory.StartNew(() => { _sherlock.InvModeSet(I_MODE.I_EXE_MODE_HALT); // Remain in loop until Sherlock has halted. while (_iCurrMode != I_MODE.I_EXE_MODE_HALT) { _iReturn = _sherlock.InvModeGet(out _iCurrMode); // Giving the cpu a second to catch its breath. Thread.Sleep(1000); } }); }
/// <summary> /// Generic method for setting a Sherlock variable value. /// </summary> /// <typeparam name="T">The Sherlock variable type.</typeparam> /// <param name="value">The value to set the Sherlock variable to.</param> /// <param name="variableName">The name of the variable in Sherlock.</param> /// <exception cref="ArgumentException"></exception> public void SetVariable <T>(string variableName, T value) { if (value is double || value is int) { _iReturn = _sherlock.VarSetDouble(variableName, Convert.ToDouble(value)); } else if (value is string) { _iReturn = _sherlock.VarSetString(variableName, Convert.ToString(value)); } else if (value is bool) { _iReturn = _sherlock.VarSetBool(variableName, Convert.ToBoolean(value)); } else if (value is double[]) { _iReturn = _sherlock.VarSetDoubleArray(variableName, ( double[] )(( object )value)); } else if (value is string[]) { _iReturn = _sherlock.VarSetStringArray(variableName, ( string[] )(( object )value)); } else if (value is bool[]) { _iReturn = _sherlock.VarSetBoolArray(variableName, ( bool[] )(( object )value)); } else { throw new ArgumentException( string.Concat("The type ", typeof(T).ToString( ), " is not supported")); } if (_iReturn != I_ENG_ERROR.I_OK) { throw new ArgumentException( string.Concat( "An issue was encountered while attemping to retrieve the value of the \"", variableName, "\" variable. Sherlock returned an error message of: ", _iReturn.ToString( ))); } }
// Initialize the Sherlock engine. private static void InitializeEngine( ) { _sherlock = new Engine( ); _iReturn = _sherlock.EngInitialize( ); }
/// <summary> /// Generic method for retrieving a Sherlock variables value. /// </summary> /// <typeparam name="T">The Sherlock variables type.</typeparam> /// <param name="variableName">The name of the variable in Sherlock.</param> /// <returns>The Sherlock variable value.</returns> /// <exception cref="ArgumentException"></exception> public T GetVariable <T>(string variableName) { object retVal; if (typeof(T) == typeof(double)) { double tempReturnVal; _iReturn = _sherlock.VarGetDouble(variableName, out tempReturnVal); retVal = Convert.ChangeType(tempReturnVal, typeof(T)); } else if (typeof(T) == typeof(string)) { string tempReturnVal; _iReturn = _sherlock.VarGetString(variableName, out tempReturnVal); retVal = Convert.ChangeType(tempReturnVal, typeof(T)); } else if (typeof(T) == typeof(bool)) { bool tempReturnVal; _iReturn = _sherlock.VarGetBool(variableName, out tempReturnVal); retVal = Convert.ChangeType(tempReturnVal, typeof(T)); } else if (typeof(T) == typeof(double[])) { Array tempReturnVal; _iReturn = _sherlock.VarGetDoubleArray(variableName, out tempReturnVal); retVal = Convert.ChangeType(tempReturnVal, typeof(T)); } else if (typeof(T) == typeof(string[])) { Array tempReturnVal; _iReturn = _sherlock.VarGetStringArray(variableName, out tempReturnVal); retVal = Convert.ChangeType(tempReturnVal, typeof(T)); } else if (typeof(T) == typeof(bool[])) { Array tempReturnVal; _iReturn = _sherlock.VarGetBoolArray(variableName, out tempReturnVal); retVal = Convert.ChangeType(tempReturnVal, typeof(T)); } else { throw new ArgumentException( string.Concat("The type ", typeof(T).ToString( ), " is not supported")); } if (_iReturn != I_ENG_ERROR.I_OK) { throw new ArgumentException( string.Concat( "An issue was encountered while attemping to retrieve the value of the \"", variableName, "\" variable. Sherlock returned an error message of: ", _iReturn.ToString( ))); } return(( T )retVal); }