Smrf.NodeXL.ExcelTemplate.GraphMetricsAggregator.TryGetGraphMetricsForOneNodeXLWorkbook C# (CSharp) Method

TryGetGraphMetricsForOneNodeXLWorkbook() protected method

protected TryGetGraphMetricsForOneNodeXLWorkbook ( String sNodeXLWorkbookFilePath, OverallMetricsInfo &oOverallMetricsInfo ) : System.Boolean
sNodeXLWorkbookFilePath String
oOverallMetricsInfo OverallMetricsInfo
return System.Boolean
    TryGetGraphMetricsForOneNodeXLWorkbook
    (
        String sNodeXLWorkbookFilePath,
        out OverallMetricsInfo oOverallMetricsInfo
    )
    {
        Debug.Assert( !String.IsNullOrEmpty(sNodeXLWorkbookFilePath) );
        AssertValid();

        oOverallMetricsInfo = null;

        // Create a new instance of Excel.  Do not use the instance that was
        // passed to AggregateGraphMetricsAsync(), because when a NodeXL
        // workbook is opened and closed in Excel, its memory is not released
        // and the machine will eventually run out of memory.

        Application oExcelApplication = new Application();

        if (oExcelApplication == null)
        {
            throw new Exception("Excel couldn't be opened.");
        }

        // ExcelApplicationKiller requires that the application be visible.

        oExcelApplication.Visible = true;

        ExcelApplicationKiller oExcelApplicationKiller =
            new ExcelApplicationKiller(oExcelApplication);

        Workbook oNodeXLWorkbook = null;

        try
        {
            oNodeXLWorkbook = ExcelUtil.OpenWorkbook(sNodeXLWorkbookFilePath,
                oExcelApplication);

            OverallMetrics oOverallMetrics;

            if ( ( new OverallMetricsReader() ).TryReadMetrics(
                oNodeXLWorkbook, out oOverallMetrics ) )
            {
                oOverallMetricsInfo = new OverallMetricsInfo();

                oOverallMetricsInfo.NodeXLWorkbookFileName =
                    Path.GetFileName(sNodeXLWorkbookFilePath);

                oOverallMetricsInfo.OverallMetrics = oOverallMetrics;

                return (true);
            }
        }
        finally
        {
            if (oNodeXLWorkbook != null)
            {
                oNodeXLWorkbook.Close(false, Missing.Value, Missing.Value);
            }

            oExcelApplication.Quit();

            // Quitting the Excel application does not remove it from
            // memory.  Kill its process.

            oExcelApplicationKiller.KillExcelApplication();
        }

        return (false);
    }