I am trying to have a clean excel break once I load a user's settings file. I am running into something I can't figure out other than it has something to do with the dictionaries. If I comment out one (or both) of the Dictionary filling brackets, the settings file loads and then releases, but if both are running, the excel app won't release. Am I also tying excel to the dictionaries by sourcing the data from them?
I am sure there are other ways to create global dictionaries, but this is the only way I am confidant with at the moment, but I am willing to learn if something better is out there.
The "dictionary filling code" is the for loops with i & j:
for (int i = 0; i < lastRow - 1; i++)
{
string key = settingsSheet.Range["B" + (i + 2)].Value;
string value = settingsSheet.Range["A" + (i + 2)].Value;
DictionaryLoad.DIC.Add(key, value);
}
here is the full code:
public Form1()
{
InitializeComponent();
txtFileNamePreface.Enabled = false;
string fileName = "F:\\Shared\\Projects\\State Assoc Clients\\Data Management\\Download Site\\KeyStats Download Statistics\\Naming Conventions.xls";
LoadProductName(fileName);
}
public static class DictionaryLoad
{
public static IDictionary<string, string> DIC;
public static IDictionary<string, string> DIC2;
static DictionaryLoad()
{
DIC = new Dictionary<string, string>();
DIC2 = new Dictionary<string, string>();
}
}
private void LoadProductName(string fileName)
{
//starting up and defining the Excel references
Excel.Application excelApp = new Excel.Application(); //excel open here
Excel.Workbook settingsBook = null;
Excel.Worksheet settingsSheet = null;
excelApp.Visible = false;
excelApp.DisplayAlerts = false;
settingsBook = excelApp.Workbooks.Open(fileName);
settingsSheet = settingsBook.Sheets["NamingConventions"];
int lastRow = findFirstBlankRow(settingsSheet, "A1") - 1;
fillComboBox(cbProductType, lastRow, settingsSheet, "A");
fillComboBox(cbYear, lastRow, settingsSheet, "D");
int lastRow2 = findFirstBlankRow(settingsSheet, "E1");
fillComboBox(cbRule, lastRow2, settingsSheet, "E");
for (int i = 0; i < lastRow - 1; i++)
{
string key = settingsSheet.Range["B" + (i + 2)].Value;
string value = settingsSheet.Range["A" + (i + 2)].Value;
DictionaryLoad.DIC.Add(key, value);
}
cbProductName.Items.Clear();
foreach (KeyValuePair<string, string> entry in DictionaryLoad.DIC)
{
if (entry.Value == cbProductType.Text)
{ cbProductName.Items.Add(entry.Key); }
}
try { cbProductName.SelectedIndex = 0; }
catch { }
for (int j = 0; j < lastRow - 1; j++)
{
string key = settingsSheet.Range["B" + (j + 2)].Value;
string value = settingsSheet.Range["C" + (j + 2)].Value;
DictionaryLoad.DIC2.Add(key, value);
}
cbRule.SelectedIndex = 0;
cbYear.Text = DateTime.Now.Year.ToString();
cbQuarter.SelectedIndex = 0;
cbMonth.Text = DateTime.Now.ToString("MMMM");
cbProductType.SelectedIndex = 0;
string workBookName = excelApp.ActiveWorkbook.FullName;
txtOutputFolder.Text = Path.GetDirectoryName(workBookName);
settingsBook.Close();
excelApp.Quit();
appCleanup(excelApp);
appCleanup(settingsBook);
appCleanup(settingsSheet);
garbageCleanup();
Application.Exit();
}
public void appCleanup(object application1, object application2 = null, object application3 = null)
{
Marshal.ReleaseComObject(application1);
application1 = null;
}
public void garbageCleanup()
{
GC.Collect();
GC.WaitForPendingFinalizers();
}