It just doesn't react at all if I hit the X button. Not even alt+f4 works
at first I read in two excel files, the read out data is used for some calculation. Edit: Could be that it is an interOp problem:
System.Data.DataTable dt = new System.Data.DataTable();
try
{
xlApp = new Excel.Application();
Workbook workbook = xlApp.Workbooks.Open(filePath);
xlBook = workbook;
dynamic xlSheet = xlBook.Worksheets[sheetName];
dynamic xlRange = xlSheet.UsedRange;
DataRow row = null;
for (int i = 1; i <= xlRange.Rows.Count; i++)
{
if (i != 1)
row = dt.NewRow();
for (int j = 1; j <= xlRange.Columns.Count; j++)
{
if (i == 1)
dt.Columns.Add(xlRange.Cells[1, j].value);
else
row[j - 1] = xlRange.Cells[i, j].value;
}
if (row != null)
dt.Rows.Add(row);
}
xlApp = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
xlBook.Close();
//xlApp.Quit();
if (xlApp != null)
{
xlApp.Quit();
xlApp = null;
}
}
Everytime you hit the add button on the form starts the calculation and adds a row to a grid view.
Edit: I triggered an event to close the app, but it still doesnt work:
private const int WM_CLOSE = 0x0010;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE)
{
var autoValidate = this.AutoValidate;
this.AutoValidate = AutoValidate.Disable;
base.WndProc(ref m);
this.AutoValidate = autoValidate;
}
else
base.WndProc(ref m);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown) return;
// Confirm user wants to close
switch (MessageBox.Show(this, "Are you sure you want to close?", "Closing", MessageBoxButtons.YesNo))
{
case DialogResult.No:
e.Cancel = true;
break;
default:
break;
}
}